Given N integers with unknown values (ai > 0) having product P. The task is to find the maximum possible greatest common divisor of these N integers.
Input : N = 3, P = 24 Output : 2 The integers will have maximum GCD of 2 when a1 = 2, a2 = 2, a3 = 6. Input : N = 2, P = 1 Output : 1 Only possibility is a1 = 1 and a2 = 1.
- First find all the prime factors of product P and store it in a Hashmap.
- The N integers will have maximum GCD when a prime factor will be common in all the integers.
- So if P = p1k1 * p2k2 * p3k3 …. where p1, p2 … are prime numbers then, maximum GCD which can be obtained will be ans = p1k1 / N * p2k2 / N * p3k3 / N ….
Below is the implementation of the above approach:
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.
- Pair of integers having least GCD among all given pairs having GCD exceeding K
- Maximum possible GCD for a pair of integers with product N
- Smallest subsequence having GCD equal to GCD of given array
- Find pair with maximum GCD for integers in range 2 to N
- Maximum possible GCD for a pair of integers with sum N
- Maximum GCD from Given Product of Unknowns
- Recursive program to print formula for GCD of n integers
- Print any pair of integers with sum of GCD and LCM equals to N
- Find a pair with maximum product in array of Integers
- Median in a stream of integers (running integers)
- Mode in a stream of integers (running integers)
- Lexicographically smallest permutation of size A having B integers exceeding all preceeding integers
- Find N integers with given difference between product and sum
- Find distinct integers for a triplet with given product
- Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k
- 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
- Check whether product of integers from a to b is positive , negative or zero
- Count of subsequences whose product is a difference of square of two integers
- Sum of product of all integers upto N with their count of divisors
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.