Magic numbers | Practice | GeeksforGeeks

Given two function, one is h(x) which is the product of all the number in an array A[ ] having size N and another
function f(x) which denotes the GCD of all the numbers in an array. Your task is to find the value of  h(x)f(x).
Note: Since the answer can be very large, use modulo 109+7.
Example 1:
Input:
N = 2, A[] = {2, 4}
Output:
64
Explanation:
h(x) = 8, f(x) = 2, Therefore, the
answer is 82 = 64.
Example 2:
Input:
N = 3, A[] = {1, 3, 7}
Output:
21
Explanation:
h(x) = 21, f(x) = 1, Therefore, the
answer is 211 = 21.

Your Task:
You don't need to read input or print anything. Your task is to complete the function getVal() which takes an Integer N and an array of integers A[] as input and returns the answer.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 105
1 <= A[i] <= 105
0
mustangg3 months ago
Why its not working!!!!
class Solution {
    static Long getVal(int N, int[] A) {
        long mult = 1;
        long po = 0;
        long mod=1000000007;
        for(int i: A)
        {
            mult*=i;
        }
        long res = 0;
        for(int i:A){
            
            res = gcd(res,i);
        }
        po =(long) Math.pow(mult,res);
    
        return po%mod;
    }
    
    static long gcd(long a,long b)
    {
        return a==0?b:gcd(b%a,a);
    }
};
0
user00375 months ago
long long gcd(long long a,long long b){
      if(b==0){
          return a;
      }
      return gcd(b,a%b);
  }
    long long getVal(int N, int A[]) {
        // code here
        
        long long g=A[0],l=A[0],mod=1000000007;
        for(int i=1;i<N;i++){
            g = gcd(g,A[i]);
            l = (l*A[i])%mod;
        }
        
        long long ans = pow(l,g);
        return ans%mod;
    }
 
 
0
Ujjwal Pandey1 year ago
Ujjwal Pandey
class Solution {    static long modulo = 1000000007 ;    static Long getVal(int N, int[] A) {        // code here        long pdct = product(A, A.length) ;        long cal = cal(A, A.length) ;        long res = 1 ;        while(cal > 0){            if((cal & 1) != 0){                res = (res%modulo * pdct%modulo) % modulo ;            }
            pdct = (pdct*pdct)%modulo ;            cal = cal >> 1 ;        }
        return res%modulo ;    }
    static long product( int[] A, int N){        long prod = 1 ;
        for(int val: A) {        prod *= (val%modulo) ;        }
        return prod%modulo ;    }
    static long cal(int[] A, int n) {long ans = A[0] ;long res = 0 ;for(int i = 1 ; i < n ; i++) {ans = (getGCD(ans, A[i])%modulo) ;
}
return (ans%modulo) ;}
static long getGCD(long a, long b) {if(b == 0)return (a%modulo) ;
return getGCD(b, a % b)%modulo ;}}
Wrong Answer. !!!Wrong Answer
Possibly your code doesn't work correctly for multiple test-cases (TCs).
The first test case where your code failed:
Input:76612759 17064 14213 26992 20182 26404 22253 26342 15533 32565 11569 16113 328 13166 20909 14412 25099 9967 16651 27337 11784 26833 6421 23518 30838 5317 18119 17505 22366 26359 23209 25488 31384 18233 30556 852 12022 3933 3698 4551 8639 4322 24451 29683 30815 7636 28028 30173 4188 6381 24123 31682 31628 20736 30809 22688 3206 11160 13127 28152 1878 25308 3485 30930 19061 23221 7598 12291 1016 20787 4192 6059 18183 28205 2619 11777 5521 30248 7871 21843 12862 9338 20987 8008 20133 28848 22619 2.................
Its Correct output is:64263939
And Your Code's output is:0
please figure out the problem
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.