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.

**Examples:**

Input:N = 4, K = 2, S = 3

Output:1 2 3 4

Explanation:

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

Explanation:

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 ] ]

**Approach:**

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:

## C++

`// C++ program to find array ` `// with K subarrays with sum S ` ` ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find array ` `// with K subarrays with sum S ` `void` `SubarraysWithSumS(` `int` `n, ` `int` `k, ` `int` `s) ` `{ ` ` ` `for` `(` `int` `i=0;i<k;i++) ` ` ` `cout << s << ` `" "` `; ` ` ` `for` `(` `int` `i=k;i<n;i++) ` ` ` `cout << s+1 << ` `" "` `; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `n = 4, k = 2, s = 3; ` ` ` ` ` `// Function call ` ` ` `SubarraysWithSumS(n, k, s); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find array ` `// with K subarrays with sum S ` `class` `GFG ` `{ ` ` ` `// Function to find array ` `// with K subarrays with sum S ` `static` `void` `SubarraysWithSumS(` `int` `n, ` `int` `k, ` `int` `s) ` `{ ` ` ` `for` `(` `int` `i = ` `0` `; i < k; i++) ` ` ` `System.out.print(s + ` `" "` `); ` ` ` `for` `(` `int` `i = k; i < n; i++) ` ` ` `System.out.print(s + ` `1` `+ ` `" "` `); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `4` `, k = ` `2` `, s = ` `3` `; ` ` ` ` ` `// Function call ` ` ` `SubarraysWithSumS(n, k, s); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find array ` `# with K subarrays with sum S ` ` ` `# Function to find array ` `# with K subarrays with sum S ` `def` `SubarraysWithSumS(n, k, s): ` ` ` `for` `i ` `in` `range` `(k): ` ` ` `print` `(s, end` `=` `" "` `) ` ` ` `for` `i ` `in` `range` `(k, n): ` ` ` `print` `(s ` `+` `1` `, end ` `=` `" "` `) ` ` ` `# Driver Code ` `n ` `=` `4` `k ` `=` `2` `s ` `=` `3` ` ` `# Function call ` `SubarraysWithSumS(n, k, s) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find array ` `// with K subarrays with sum S ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find array ` `// with K subarrays with sum S ` `static` `void` `SubarraysWithSumS(` `int` `n, ` `int` `k, ` `int` `s) ` `{ ` ` ` `for` `(` `int` `i = 0; i < k; i++) ` ` ` `Console.Write(s + ` `" "` `); ` ` ` `for` `(` `int` `i = k; i < n; i++) ` ` ` `Console.Write(s + 1 + ` `" "` `); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `n = 4, k = 2, s = 3; ` ` ` ` ` `// Function call ` ` ` `SubarraysWithSumS(n, k, s); ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

**Output:**

3 3 4 4

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.

## Recommended Posts:

- Number of subarrays having sum exactly equal to k
- Count of subarrays having exactly K prime numbers
- Count of subarrays having exactly K perfect square numbers
- Count of subarrays having exactly K distinct elements
- Maximum subarray size, such that all subarrays of that size have sum less than k
- Count of subarrays of size K with elements having even frequencies
- Count of subarrays of size K having at least one pair with absolute difference divisible by K-1
- Find the longest sub-array having exactly k odd numbers
- Maximize the size of array by deleting exactly k sub-arrays to make array prime
- Count of ways to split an Array into three contiguous Subarrays having increasing Sum
- Generate Array whose sum of all K-size subarrays divided by N leaves remainder X
- Count of subarrays which contains a given number exactly K times
- Find the String having each substring with exactly K distinct characters
- First subarray having sum at least half the maximum sum of any subarray of size K
- Number of subarrays having absolute sum greater than K | Set-2
- Number of subarrays having sum less than K
- Number of subarrays having sum in a given range
- Number of subarrays having sum of the form k^m, m >= 0
- Count subarrays having sum of elements at even and odd positions equal
- Count of subarrays having sum equal to its length

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.