Given a positive integer N and K, the task is to divide N into K parts such that the first part has a value X, the second part is 2X, and so on for some value of X. If such division is not possible then print -1.
Input: N = 10, K = 4
Output: 1 2 3 4
If we take 1 as first number second number will be 2 third number will be 3 times first which is 3 and the last number will be 4 times third number so last number is 4. We can note that sum=1+2+3+4=10 which is the required sum.
Input N = 10, K = 3
Distributing N in 3 parts with given constraint is not possible.
Approach: To solve the problem mentioned above let’s to understand it’s mathematical implementation. Let the division be X1, X2, X3 up to XK where the second integer is X1 * 2, third one is X1 * 3 and the Kth one is X1 * K.
We know that,
=> , where ( 1 + 2 + 3 + … + K) =
So to solve the problem we have to follow the steps given below:
- Calculate the value of K * (K + 1) and divide 2 * N by K * (K + 1) in order to get value of X1.
- If X1 is not an integer in the above step then print -1 as there is no such division is possible.
- To get the value of X2 we will multiply X1 by 2. Similarly, to get XK multiply X1 with K.
- After finding all teh values print them.
Below is the implementation of above approach:
1 2 3 4
Time Complexity: O(K)
Auxiliary Space: O(1)
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.
- Divide N into K unique parts such that gcd of those parts is maximum
- Split a number into 3 parts such that none of the parts is divisible by 3
- Divide number into two parts divisible by given numbers
- Divide a big number into two parts that differ by k
- Divide a number into two parts such that sum of digits is maximum
- Minimum cuts required to divide the Circle into equal parts
- Find the number of ways to divide number into four parts such that a = c and b = d
- Divide a number into two parts
- 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
- Partition a number into two divisible parts
- Break the number into three parts
- 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
- Check if an array of 1s and 2s can be divided into 2 parts with equal sum
- Sum of all second largest divisors after splitting a number into one or more parts
- Number of ways N can be divided into four parts to construct a rectangle
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.