Given three integers N, K and S, the task is to choose an array of size N such that there exists exactly K sub-arrays with sum S.
Note: There can be many solution arrays to this problem.
Input: N = 4, K = 2, S = 3
Output: 1 2 3 4
One of the possible array is [ 1, 2, 3, 4 ]
There exist exactly two subarrays with sum 3
Subarrays with Sum(3) = [ [ 1, 2 ], [ 3 ] ]
Input: N = 5, K = 3, S = 50
Output: 25 25 25 10 40
One of the possible array is [ 25, 25, 25, 10, 40 ]
There exist exactly three subarrays with sum 50
Subarrays with Sum(50) = [ [ 25, 25 ], [ 25, 25 ], [ 10, 40 ] ]
One of the Solution Array for this problem contains S element K times and S+1 element (N-K) times, to form K Sub-arrays of exactly one element with S as sum. If we combine any two or more elements of the array then it will give sum greater than S.
Below is the implementation of the above approach:
3 3 4 4
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Minimum flips in a Binary array such that XOR of consecutive subarrays of size K have different parity
- Maximum subarray size, such that all subarrays of that size have sum less than k
- Find if array can be divided into two subarrays of equal sum
- Find an element which divides the array in two subarrays with equal product
- Sum of all subarrays of size K
- Maximum of all Subarrays of size k using set in C++ STL
- Max sum of M non-overlapping subarrays of size K
- Minimum and Maximum of all subarrays of size K using Map
- Maximum sum two non-overlapping subarrays of given size
- Counting inversions in all subarrays of given size
- Sum of minimum and maximum elements of all subarrays of size k.
- Maximum of all subarrays of size K using Segment Tree
- Find Maximum XOR value of a sub-array of size k
- Find if there is any subset of size K with 0 sum in an array of -1 and +1
- Count non decreasing subarrays of size N from N Natural numbers
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.