313. Super Ugly Number
A super ugly number is a positive integer whose prime factors are in the array primes.
Given an integer n and an array of integers primes, return the n t h n^{th} nth super ugly number.
The  
     
      
       
        
        
          n 
         
         
         
           t 
          
         
           h 
          
         
        
       
      
        n^{th} 
       
      
    nth super ugly number is guaranteed to fit in a 32-bit signed integer.
  
Example 1:
Example 2:
Constraints:
- 1 < = n < = 1 0 5 1 <= n <= 10^5 1<=n<=105
 - 1 <= primes.length <= 100
 - 2 <= primes[i] <= 1000
 - primes[i] is guaranteed to be a prime number.
 - All the values of primes are unique and sorted in ascending order.
 
From: LeetCode
 Link: 313. Super Ugly Number
Solution:
Ideas:
- nextUgly: Changed the type from int to long long to prevent overflow when calculating the next ugly number.
 - Casting: When assigning the value back to the ugly array, cast the result to int to match the expected return type and prevent further issues.
 
Code:
// Function to find the n-th super ugly number
int nthSuperUglyNumber(int n, int* primes, int primesSize) {
    int* ugly = (int*)malloc(n * sizeof(int));
    int* index = (int*)malloc(primesSize * sizeof(int));
    long long* nextUgly = (long long*)malloc(primesSize * sizeof(long long));
    
    ugly[0] = 1;
    
    for (int i = 0; i < primesSize; i++) {
        index[i] = 0;
        nextUgly[i] = primes[i];
    }
    
    for (int i = 1; i < n; i++) {
        long long next = nextUgly[0];
        for (int j = 1; j < primesSize; j++) {
            if (nextUgly[j] < next) {
                next = nextUgly[j];
            }
        }
        ugly[i] = (int)next;
        
        for (int j = 0; j < primesSize; j++) {
            if (nextUgly[j] == next) {
                index[j]++;
                nextUgly[j] = (long long)ugly[index[j]] * primes[j];
            }
        }
    }
    
    int result = ugly[n - 1];
    free(ugly);
    free(index);
    free(nextUgly);
    return result;
}










