Create an array of size N with sum S such that no subarray exists with sum S or S-K
Last Updated :
23 Nov, 2021
Given a number N and an integer S, the task is to create an array of N integers such that sum of all elements equals to S and print an element K where 0 ? K ? S, such that there exists no subarray with sum equals to K or (S – K).
If no such array is possible then print “-1”.
Note: There can be more than one value for K. You can print any one of them.
Examples:
Input: N = 1, S = 4
Output: {4}
K = 2
Explanation:
There exists an array {4} whose sum is 4.
From all possible value of K i.e., 0 ? K ? 4, K = 1, 2, and 3 satisfy the given conditions.
For K = 2, there is no subarray whose sum is 2 or S – K i.e., 4 – 2 = 2.
Input: N = 3, S = 8
Output: {2, 2, 4}
K = 1
Explanation:
There exists an array {2, 2, 4} and there exists K as 1 such that there is no subarray whose sum is 1 and S – K i.e., 8 – 1 = 7.
Approach: To solve the problem mentioned above we have to observe that:
- If 2 * N > S then there is no array possible.
For Example:
For N = 3 and S = 4, then the possible arrays are {1, 2, 1}, {1, 1, 2}, {2, 1, 1}.
The possible values for K are 0, 1, 2, 3 (0 < = k < = S).
But there is no value for K which satisfy the condition.
So the solution to this is not possible.
- An array is only possible if 2 * N <= S and the array can be created using elements (N-1) times 2 and the last element as S – (2 * (N – 1)) and K will always be 1.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
void createArray( int n, int s)
{
if (2 * n <= s)
{
for ( int i = 0; i < n - 1; i++)
{
cout << "2" << " " ;
s -= 2;
}
cout << s << endl;
cout << "1" << endl;
}
else
cout << "-1" << endl;
}
int main()
{
int N = 1;
int S = 4;
createArray(N, S);
}
|
Java
class GFG{
static void createArray( int n, int s)
{
if ( 2 * n <= s)
{
for ( int i = 0 ; i < n - 1 ; i++)
{
System.out.print( 2 + " " );
s -= 2 ;
}
System.out.println(s);
System.out.println( 1 );
}
else
System.out.print( "-1" );
}
public static void main(String[] args)
{
int N = 1 ;
int S = 4 ;
createArray(N, S);
}
}
|
Python3
def createArray(n, s):
if ( 2 * n< = s):
for i in range (n - 1 ):
print ( 2 , end = " " )
s - = 2
print (s)
print ( 1 )
else :
print ( '-1' )
N = 1
S = 4
createArray(N, S)
|
C#
using System;
class GFG{
static void createArray( int n, int s)
{
if (2 * n <= s)
{
for ( int i = 0; i < n - 1; i++)
{
Console.Write(2 + " " );
s -= 2;
}
Console.WriteLine(s);
Console.WriteLine(1);
}
else
Console.Write( "-1" );
}
public static void Main()
{
int N = 1;
int S = 4;
createArray(N, S);
}
}
|
Javascript
<script>
function createArray(n, s)
{
if (2 * n <= s)
{
for (let i = 0; i < n - 1; i++)
{
document.write(2 + " " );
s -= 2;
}
document.write(s + "<br/>" );
document.write(1);
}
else
document.write( "-1" );
}
let N = 1;
let S = 4;
createArray(N, S);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...