Given an integer n, print m increasing numbers such that the sum of m numbers is equal to n and the GCD of m numbers is maximum among all series possible. If no series is possible then print “-1”.
Input : n = 24, m = 3 Output : 4 8 12 Explanation : (3, 6, 15) is also a series of m numbers which sums to N, but gcd = 3 (4, 8, 12) has gcd = 4 which is the maximum possible. Input : n = 6 m = 4 Output : -1 Explanation: It is not possible as the least GCD sequence will be 1+2+3+4 which is greater then n, hence print -1.
The most common observation is that the gcd of the series will always be a divisor of n. The maximum gcd possible (say b) will be n/sum, where sum is the sum of 1+2+..m.
If b turns out to be 0, then the sum of 1+2+3..+k exceeds n which is invalid, hence output “-1”.
Traverse to find out all the divisors possible, a loop till sqrt(n). If the current divisor is i, the best possible way to take the series will be to consider i, 2*i, 3*i, …(m-1)*i, and their sum is s which is equal to i * (m*(m-1))/2 . The last number will be n-s.
Along with i being the divisor, n/i will be the other divisor so check for that also.
Take maximum of possible divisor possible (say r) which should be less then or equals to b and print the sequence as r, 2*r, … (m-1)*r, n—s.
If no such divisors are found simply output “-1”.
2 4 6 12 1 2 3 4 14 -1
Time complexity: O( sqrt (n) )
Auxiliary Space: O(1)
This article is contributed by Raja Vikramaditya. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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.
- Print any pair of integers with sum of GCD and LCM equals to N
- Find the number of pairs such that their gcd is equals to 1
- Count of numbers upto M with GCD equals to K when paired with M
- Pair of integers having least GCD among all given pairs having GCD exceeding K
- Largest subarray with GCD one
- Largest subsequence having GCD greater than 1
- Largest Subset with GCD 1
- Lexicographically largest permutation of the array such that a[i] = a[i-1] + gcd(a[i-1], a[i-2])
- Count of n digit numbers whose sum of digits equals to given sum
- Print all n-digit numbers whose sum of digits equals to given sum
- Count numbers whose maximum sum of distinct digit-sum is less than or equals M
- Array containing power of 2 whose XOR and Sum of elements equals X
- Find two numbers whose sum and GCD are given
- Count of all possible pairs having sum of LCM and GCD equal to N
- Minimum number of squares whose sum equals to given number n
- Minimum number of squares whose sum equals to given number N | set 2
- Minimum number of squares whose sum equals to a given number N | Set-3
- Sum of series formed by difference between product and sum of N natural numbers
- Sum of GCD of all numbers upto N with N itself
- Calculate the Sum of GCD over all subarrays