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
- Generate elements of the array following given conditions
- 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
- 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
- Elements to be added so that all elements of a range are present in array
- Find elements larger than half of the elements in an array
- Generate minimum sum sequence of integers with even elements greater
- Find Kth element in an array containing odd elements first and then even elements
- Replace array elements by sum of next two consecutive elements
- Maximize the sum of X+Y elements by picking X and Y elements from 1st and 2nd array
- Count number of elements between two given elements in array
- 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 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.