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
- 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 elements of the array following given conditions
- Choose X elements from A and Y elements from B which satisfy the given condition
- Generate original array from an array that store the counts of greater elements on right
- Generate original array from difference between every two consecutive elements
- Average of remaining elements after removing K largest and K smallest elements from array
- Rearrange array elements such that Bitwise AND of first N - 1 elements is equal to last element
- Minimum sum of the elements of an array after subtracting smaller elements from larger
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Find elements larger than half of the elements in an array
- Elements to be added so that all elements of a range are present in array
- Value to be subtracted from array elements to make sum of all elements equals K
- Generate minimum sum sequence of integers with even elements greater
- Find all elements in array which have at-least two greater elements
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.