Check if L sized Subarray of first N numbers can have sum S with one deletion allowed
Last Updated :
18 Sep, 2023
Consider an integer sequence A = {1, 2, 3, …., N} i.e. the first N natural numbers in order and two integers, L and S. Check whether there exists a subarray of length L and sum S after removing at most one element from A.
Examples:
Input: N = 5, L = 3, S = 11
Output: YES
Explanation: We can remove 3 from A to obtain A = {1, 2, 4, 5} where {2, 4, 5} is a required subarray of size 3 and sum 11.
Input: N = 5, L = 3, S = 5
Output: NO
Explanation: For the above input its not possible to determine a subarray with the required conditions.
Approach: The problem can be solved based on the following observation:
The observations are
- The first observation is that if the given sum is less than the minimum sum or greater than the maximum sum then it is obvious that we can’t form or find a subarray. The minimum sum would be the sum of the first L natural numbers and the maximum sum would be the sum of the last L natural numbers.
- The second observation is that if the given sum is in between the minimum and maximum sum then it is always possible to obtain the sum because the sequence contains all the numbers in the natural numbers order so that we can always make a sub-array with the given sum provided by removing at most one element from the sequence.
Follow the steps mentioned below to implement the idea:
- First find out the sum of the first L natural numbers and the sum of the last L natural numbers.
- The sum of the first N natural numbers (N * (N + 1))/2.
- So the sum of the last L natural numbers can be found like this: sum of first N natural numbers – sum of first (N-L) natural numbers.
- Check if S lies within the range of [sum of first L natural number, sum of last L natural numbers]. If the condition is satisfied then the sum exists.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int findSum( int n)
{
return n * (n + 1) / 2;
}
bool isPossible( int n, int l, int s)
{
int minimumSum = findSum(l);
int maximumSum = findSum(n) - findSum(n - l);
if (s < minimumSum || s > maximumSum)
return false ;
return true ;
}
int main()
{
int N = 5, L = 3, S = 11;
if (isPossible(N, L, S))
cout << "YES" ;
else
cout << "NO" ;
return 0;
}
|
Java
public class GFG {
static int findSum( int n) {
return n * (n + 1 ) / 2 ;
}
static boolean isPossible( int n, int l, int s) {
int minimumSum = findSum(l);
int maximumSum = findSum(n) - findSum(n - l);
if (s < minimumSum || s > maximumSum)
return false ;
return true ;
}
public static void main(String[] args) {
int N = 5 , L = 3 , S = 11 ;
if (isPossible(N, L, S))
System.out.println( "YES" );
else
System.out.println( "NO" );
}
}
|
Python3
def findSum(n):
return n * (n + 1 ) / / 2
def isPossible(n, l, s):
minimumSum = findSum(l)
maximumSum = findSum(n) - findSum(n - l)
if s < minimumSum or s > maximumSum:
return False
return True
N = 5
L = 3
S = 11
if isPossible(N, L, S):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
class GFG
{
static int FindSum( int n)
{
return n * (n + 1) / 2;
}
static bool IsPossible( int n, int l, int s)
{
int minimumSum = FindSum(l);
int maximumSum = FindSum(n) - FindSum(n - l);
if (s < minimumSum || s > maximumSum)
return false ;
return true ;
}
static void Main( string [] args)
{
int N = 5, L = 3, S = 11;
if (IsPossible(N, L, S))
Console.WriteLine( "YES" );
else
Console.WriteLine( "NO" );
}
}
|
Javascript
function findSum(n) {
return n * (n + 1) / 2;
}
function isPossible(n, l, s) {
let minimumSum = findSum(l);
let maximumSum = findSum(n) - findSum(n - l);
if (s < minimumSum || s > maximumSum)
return false ;
return true ;
}
let N = 5, L = 3, S = 11;
if (isPossible(N, L, S))
console.log( "YES" );
else
console.log( "NO" );
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...