Check if a subarray exists with sum greater than the given Array
Last Updated :
19 May, 2021
Given an array of integers arr, the task is to check if there is a subarray (except the given array) such that the sum of its elements is greater than or equal to the sum of elements of the given array. If no such subarray is possible, print No, else print Yes.
Examples:
Input: arr = {5, 6, 7, 8}
Output: No
Explanation:
There isn’t any subarray of the given array such that sum of its elements is greater than or equal to the sum of elements of given array.
Input: arr = {-1, 7, 4}
Output: Yes
Explanation:
There exist a subarray {7, 4} whose sum is greater than the sum of elements of given array.
Approach: Subarray with sum greater than the sum of original array is possible only in one of two conditions
- If the sum of all elements of the given array is less than or equal to 0
- If there exists a prefix or suffix subarray whose sum is negative
So check if the sum of all possible prefix and suffix subarray is less than or equal to zero, the answer is Yes. Else the answer is No.
Below is the implementation of the above approach
C++
#include <bits/stdc++.h>
using namespace std;
int subarrayPossible( int arr[], int n)
{
int sum = 0;
for ( int i = 0; i < n; i++) {
sum += arr[i];
if (sum <= 0)
return 1;
}
sum = 0;
for ( int i = n - 1; i >= 0; i--) {
sum += arr[i];
if (sum <= 0)
return 1;
}
return 0;
}
int main()
{
int arr[] = { 10, 5, -12, 7, -10, 20,
30, -10, 50, 60 };
int size = sizeof (arr) / sizeof (arr[0]);
if (subarrayPossible(arr, size))
cout << "Yes"
<< "\n" ;
else
cout << "No"
<< "\n" ;
return 0;
}
|
Java
import java.util.*;
class GFG{
static boolean subarrayPossible( int arr[], int n)
{
int sum = 0 ;
for ( int i = 0 ; i < n; i++) {
sum += arr[i];
if (sum <= 0 )
return true ;
}
sum = 0 ;
for ( int i = n - 1 ; i >= 0 ; i--) {
sum += arr[i];
if (sum <= 0 )
return true ;
}
return false ;
}
public static void main(String args[])
{
int arr[] = { 10 , 5 , - 12 , 7 , - 10 , 20 , 30 , - 10 , 50 , 60 };
int size = arr.length;
if (subarrayPossible(arr, size))
System.out.print( "Yes" + "\n" );
else
System.out.print( "No" + "\n" );
}
}
|
Python3
def subarrayPossible(arr, n):
sum = 0 ;
for i in range (n):
sum + = arr[i];
if ( sum < = 0 ):
return True ;
sum = 0 ;
for i in range (n - 1 , - 1 , - 1 ):
sum + = arr[i];
if ( sum < = 0 ):
return True ;
return False ;
if __name__ = = '__main__' :
arr = [ 10 , 5 , - 12 , 7 , - 10 , 20 , 30 , - 10 , 50 , 60 ];
size = len (arr);
if (subarrayPossible(arr, size)):
print ( "Yes" );
else :
print ( "No" );
|
C#
using System;
class GFG{
static bool subarrayPossible( int []arr, int n)
{
int sum = 0;
for ( int i = 0; i < n; i++) {
sum += arr[i];
if (sum <= 0)
return true ;
}
sum = 0;
for ( int i = n - 1; i >= 0; i--) {
sum += arr[i];
if (sum <= 0)
return true ;
}
return false ;
}
public static void Main(String []args)
{
int []arr = { 10, 5, -12, 7, -10, 20, 30, -10, 50, 60 };
int size = arr.Length;
if (subarrayPossible(arr, size))
Console.Write( "Yes" + "\n" );
else
Console.Write( "No" + "\n" );
}
}
|
Javascript
<script>
function subarrayPossible(arr, n)
{
let sum = 0;
for (let i = 0; i < n; i++) {
sum += arr[i];
if (sum <= 0)
return true ;
}
sum = 0;
for (let i = n - 1; i >= 0; i--) {
sum += arr[i];
if (sum <= 0)
return true ;
}
return false ;
}
let arr = [ 10, 5, -12, 7, -10, 20, 30, -10, 50, 60 ];
let size = arr.length;
if (subarrayPossible(arr, size))
document.write( "Yes" + "<br/>" );
else
document.write( "No" + "<br/>" );
</script>
|
Performance Analysis:
- Time Complexity: In the above approach, we are iterating over the array of length N twice, so the time complexity is O(N).
- Auxiliary Space Complexity: In the above approach, we are using only a few constants, so auxiliary space complexity is O(1).
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...