Given two integers N and K, the task is to generate an array arr of length K such that:
- arr + arr + … + arr[K – 1] = N.
- arr[i] > 0 for 0 ≤ i < K.
- arr[i] < arr[i + 1] ≤ 2 * arr[i] for 0 ≤ i < K – 1.
If there are multiple answers find any one of them, otherwise, print -1.
Input: N = 26, K = 6
Output: 1 2 3 4 6 10
The above array satisfies all the conditions.
Input: N = 8, k = 3
Approach: Initially we form the array with the lowest possible configuration which is filling up the array with 1, 2, 3, 4.. which satisfies the given conditions. If the summation of 1..K is greater than N, then the array cannot be formed. In order to form the array, fill up the array initially with 1, 2, 3, .. K. Again add (n-sum)/k to every element in the array, because in adding so, no conditions are void, because we are adding equal elements to every number.
The remaining number rem is greedily added from the back, to make every number twice of its previous number. After filling up the array, if any of the given conditions are not met, print -1, else the formed array will be our desired answer.
Below is the implementation of the above approach:
1 2 3 4 6 10
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.
- Generate an Array such with elements maximized through swapping bits
- Generate Complete Binary Tree in such a way that sum of non-leaf nodes is minimum
- Divide N segments into two non-empty groups such that given condition is satisfied
- Minimum changes required such that the string satisfies the given condition
- Partition the digits of an integer such that it satisfies a given condition
- Generate two BSTs from the given array such that maximum height among them is minimum
- Choose X elements from A and Y elements from B which satisfy the given condition
- Minimize the sum of the array according the given condition
- Sum of absolute differences of pairs from the given array that satisfy the given condition
- Generate an Array in which count of even and odd sum sub-arrays are E and O respectively
- Generate an array of given size with equal count and sum of odd and even numbers
- Largest sub-set possible for an array satisfying the given condition
- Check if elements of an array can be arranged satisfying the given condition
- Append two elements to make the array satisfy the given condition
- Generate original array from an array that store the counts of greater elements on right
- Find an element in array such that sum of left array is equal to sum of right array
- Maximum path sum for each position with jumps under divisibility condition
- Find the maximum sum (a+b) for a given input integer N satisfying the given condition
- Find subset with maximum sum under given condition
- Perform range sum queries on string as per given condition
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.