Find an array of size N having exactly K subarrays with sum S
Last Updated :
11 Jul, 2022
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;
}
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
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
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
JavaScript
<script>
// Javascript program to find array
// with K subarrays with sum S
// Function to find array
// with K subarrays with sum S
function SubarraysWithSumS(n,k,s)
{
for(let i = 0; i < k; i++)
document.write(s + " ");
for(let i = k; i < n; i++)
document.write(s + 1 + " ");
}
// Driver Code
let n = 4, k = 2, s = 3;
// Function call
SubarraysWithSumS(n, k, s);
// This code is contributed by unknown2108
</script>
Time Complexity: O(n)
Auxiliary Space: O(1)
Related Topic: Subarrays, Subsequences, and Subsets in Array
Similar Reads
Build Array of length N such that exactly X Subarrays have positive sums Given integers N and X, the task is to construct an array of size N such that exactly X subarrays have positive sums and other subarrays have negative sums. Note: If multiple subarrays are possible, print any of them. Examples: Input: N = 3, X = 2Output: 2 -1 -2Explanation: [0, 0] and [0, 1] subarra
9 min read
Count of Subarrays with sum equals k in given Binary Array Given a binary array arr[] and an integer k, the task is to find the count of non-empty subarrays with a sum equal to k.Examples:Input: arr[] = {1, 0, 1, 1, 0, 1}, k = 2Output: 6Explanation: All valid subarrays are: {1, 0, 1}, {0, 1, 1}, {1, 1}, {1, 0, 1}, {0, 1, 1, 0}, {1, 1, 0}.Input: arr[] = {0,
10 min read
Subarray with exactly K positive sum Given two integer values N and K, the task is to create an array A of N integers such that number of the total positive sum subarrays is exactly K and the remaining subarrays have a negative sum (-1000 ⤠A[i] ⤠1000). Examples: Input: N = 4, K=5Output: 2 2 -1 -1000Explanation:Positive Sum Subarrays:
8 min read
Subarray of size k with given sum Given an array arr[], an integer K and a Sum. The task is to check if there exists any subarray with K elements whose sum is equal to the given sum. If any of the subarray with size K has the sum equal to the given sum then print YES otherwise print NO. Examples: Input: arr[] = {1, 4, 2, 10, 2, 3, 1
10 min read
Number of subarrays having absolute sum greater than K | Set-2 Given an integer array arr[] of length N consisting of both positive and negative integers, the task is to find the number of sub-arrays with the absolute value of sum greater than a given positive number K. Examples: Input : arr[] = {-1, 0, 1}, K = 0 Output : 4 All possible sub-arrays and there tot
6 min read