Given two integers N and P where P is the product of N unknown integers, the task is to find the GCD of those integers. There can be different group of integers possible that give the same product, in that case print the GCD which is maximum among all possible groups.
Examples:
Input: N = 3, P = 24
Output: 2
{1, 1, 24}, {1, 2, 12}, {1, 3, 8}, {1, 4, 6}, {2, 2, 6} and {2, 3, 4}
are the only integer groups possible with product = 24
And they have GCDs 1, 1, 1, 1, 2 and 1 respectively.Input: N = 5, P = 1
Output:
Approach: Let g be the gcd of a1, a2, a3, …, an. Since, ai must be a multiple of g for each i and their product P = a1 * a2 * a3 * … * an must be a multiple of gn. The answer is the maximum g such that gn % P = 0.
Let P = k1p1 * k2p2 * k3p3 * … * knpt. Then g must be of the form k1p1‘ * k2p2‘ * k3p3‘ * … * knpt‘. In order to maximise g we must choose pi‘ = pi / N
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include<bits/stdc++.h> using namespace std; // Function to return the required gcd long max_gcd(long n, long p) { int count = 0; long gcd = 1; // Count the number of times 2 divides p while (p % 2 == 0) { // Equivalent to p = p / 2; p >>= 1; count++; } // If 2 divides p if (count > 0) gcd *= (long)pow(2, count / n); // Check all the possible numbers // that can divide p for (long i = 3; i <= sqrt(p); i += 2) { count = 0; while (p % i == 0) { count++; p = p / i; } if (count > 0) { gcd *= (long)pow(i, count / n); } } // If n in the end is a prime number if (p > 2) gcd *= (long)pow(p, 1 / n); // Return the required gcd return gcd; } // Driver code int main() { long n = 3; long p = 80; cout << max_gcd(n, p); } // This code is contributed by Code_Mech |
Java
// Java implementation of the approach class GFG { // Function to return the required gcd static long max_gcd(long n, long p) { int count = 0; long gcd = 1; // Count the number of times 2 divides p while (p % 2 == 0) { // Equivalent to p = p / 2; p >>= 1; count++; } // If 2 divides p if (count > 0) gcd *= (long)Math.pow(2, count / n); // Check all the possible numbers // that can divide p for (long i = 3; i <= Math.sqrt(p); i += 2) { count = 0; while (p % i == 0) { count++; p = p / i; } if (count > 0) { gcd *= (long)Math.pow(i, count / n); } } // If n in the end is a prime number if (p > 2) gcd *= (long)Math.pow(p, 1 / n); // Return the required gcd return gcd; } // Driver code public static void main(String[] args) { long n = 3; long p = 80; System.out.println(max_gcd(n, p)); } } |
Python3
# Python3 implementation of the approach import math # Function to return the required gcd def max_gcd(n, p): count = 0; gcd = 1; # Count the number of times 2 divides p while (p % 2 == 0): # Equivalent to p = p / 2; p >>= 1; count = count + 1; # If 2 divides p if (count > 0): gcd = gcd * pow(2, count // n); # Check all the possible numbers # that can divide p for i in range(3, (int)(math.sqrt(p)), 2): count = 0; while (p % i == 0): count = count + 1; p = p // i; if (count > 0): gcd = gcd * pow(i, count // n); # If n in the end is a prime number if (p > 2) : gcd = gcd * pow(p, 1 // n); # Return the required gcd return gcd; # Driver code n = 3; p = 80; print(max_gcd(n, p)); # This code is contributed by Shivi_Aggarwal |
C#
// C# implementation of the approach using System; class GFG { // Function to return the required gcd static long max_gcd(long n, long p) { int count = 0; long gcd = 1; // Count the number of times 2 divides p while (p % 2 == 0) { // Equivalent to p = p / 2; p >>= 1; count++; } // If 2 divides p if (count > 0) gcd *= (long)Math.Pow(2, count / n); // Check all the possible numbers // that can divide p for (long i = 3; i <= Math.Sqrt(p); i += 2) { count = 0; while (p % i == 0) { count++; p = p / i; } if (count > 0) { gcd *= (long)Math.Pow(i, count / n); } } // If n in the end is a prime number if (p > 2) gcd *= (long)Math.Pow(p, 1 / n); // Return the required gcd return gcd; } // Driver code public static void Main() { long n = 3; long p = 80; Console.WriteLine(max_gcd(n, p)); } } // This code is contributed by Ryuga |
PHP
<?php // PHP implementation of the approach // Function to return the required gcd function max_gcd($n, $p) { $count = 0; $gcd = 1; // Count the number of times 2 divides p while ($p % 2 == 0) { // Equivalent to p = p / 2; $p >>= 1; $count++; } // If 2 divides p if ($count > 0) $gcd *= pow(2, (int)($count / $n)); // Check all the possible numbers // that can divide p for ($i = 3; $i <= (int)sqrt($p); $i += 2) { $count = 0; while ($p % $i == 0) { $count++; $p = (int)($p / $i); } if ($count > 0) { $gcd *= pow($i, (int)($count / $n)); } } // If n in the end is a prime number if ($p > 2) $gcd *= pow($p, (int)(1 / $n)); // Return the required gcd return $gcd; } // Driver code $n = 3; $p = 80; echo(max_gcd($n, $p)); // This code is contributed by Code_Mech |
2
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Pair of integers having least GCD among all given pairs having GCD exceeding K
- Smallest subsequence having GCD equal to GCD of given array
- Maximum GCD of N integers with given product
- Maximum possible GCD for a pair of integers with product N
- Maximum possible GCD after replacing at most one element in the given array
- Find M such that GCD of M and given number N is maximum
- Replace every matrix element with maximum of GCD of row or column
- Find pair with maximum GCD in an array
- Pair with maximum GCD from two arrays
- Subsequence of size k with maximum possible GCD
- Find a positive number M such that gcd(N^M, N&M) is maximum
- Longest sub-sequence with maximum GCD
- Longest sub-array with maximum GCD
- Divide N into K unique parts such that gcd of those parts is maximum
- Find the maximum GCD of the siblings of a Binary Tree
- Maximum GCD of all subarrays of length at least 2
- Length of longest subarray of length at least 2 with maximum GCD
- Find pair with maximum GCD for integers in range 2 to N
- Maximum possible GCD for a pair of integers with sum N
- Find maximum GCD value from root to leaf in a Binary tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

