Given a positive integer N, the task is to divide it into K unique parts such that the sum of these parts is equal to the original number and the gcd of all the parts is maximum. Print the maximum gcd if such a division exists else print -1.
Input: N = 6, K = 3
Only possible division with unique
elements is (1, 2, 3) and gcd(1, 2, 3) = 1.
Input: N = 18, K = 3
Naive approach: Find all the possible divisions of N and compute the maximum gcd of them. But this approach will take exponential time and space.
Efficient approach: Let the divisions of N be A1, A2……..AK
Now, it is known that gcd(a, b) = gcd(a, b, a + b) and hence, gcd(A1, A2….AK) = gcd(A1, A2….AK, A1 + A2…. + AK)
But A1 + A2…. + AK = N and hence, the gcd of the divisions will be one of the factors of N.
Let a be the factor of N which can be the possible answer:
Since a is the gcd, all the division will be the multiples of a and hence, for K unique Bi,
a * B1 + a * B2……. + a * BK = N
a * (B1 + B2…….+ BK) = N
Since all the Bi are unique,
B1 + B2…….+ BK ≥ 1 + 2 + 3 ….. + K
B1 + B2…….+ BK ≥ K * (K + 1) / 2
Hence, all the factors of N whose complementary factor is greater than or equal to K * (K + 1) / 2 can be one of the possible answers and we have take to the maximum of all the possible answers.
Below is the implementation of the above approach:
- Divide a number into two parts such that sum of digits is maximum
- Split a number into 3 parts such that none of the parts is divisible by 3
- Find the number of ways to divide number into four parts such that a = c and b = d
- Divide number into two parts divisible by given numbers
- Divide a big number into two parts that differ by k
- Minimum cuts required to divide the Circle into equal parts
- Divide a number into two parts
- Divide array into two parts with equal sum according to the given constraints
- Divide N into K parts in the form (X, 2X, ... , KX) for some value of X
- Count of maximum occurring subsequence using only those characters whose indices are in GP
- Split a string in equal parts such that all parts are palindromes
- Find the sum of numbers from 1 to n excluding those which are powers of K
- Split the number into N parts such that difference between the smallest and the largest part is minimum
- Minimum Cuts can be made in the Chessboard such that it is not divided into 2 parts
- Partition N into M parts such that difference between Max and Min part is smallest
- Split the string into minimum parts such that each part is in the another string
- Count number of ways to divide a number in 4 parts
- Divide an isosceles triangle in two parts with ratio of areas as n:m
- Count ways to divide C in two parts and add to A and B to make A strictly greater than B
- Divide a sorted array in K parts with sum of difference of max and min minimized in each part
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.