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++
// C++ program to check if a subarray exists
// with sum greater than the given Array
#include <bits/stdc++.h>
using namespace std;
// Function to check whether there exists
// a subarray whose sum is greater than
// or equal to sum of given array elements
int subarrayPossible(int arr[], int n)
{
// Initialize sum with 0
int sum = 0;
// Checking possible prefix subarrays.
// If sum of them is less than or equal
// to zero, then return 1
for (int i = 0; i < n; i++) {
sum += arr[i];
if (sum <= 0)
return 1;
}
// again reset sum to zero
sum = 0;
// Checking possible suffix subarrays.
// If sum of them is less than or equal
// to zero, then return 1
for (int i = n - 1; i >= 0; i--) {
sum += arr[i];
if (sum <= 0)
return 1;
}
// Otherwise return 0
return 0;
}
// Driver Code
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
// Java program to check if a subarray exists
// with sum greater than the given Array
import java.util.*;
class GFG{
// Function to check whether there exists
// a subarray whose sum is greater than
// or equal to sum of given array elements
static boolean subarrayPossible(int arr[], int n)
{
// Initialize sum with 0
int sum = 0;
// Checking possible prefix subarrays.
// If sum of them is less than or equal
// to zero, then return 1
for (int i = 0; i < n; i++) {
sum += arr[i];
if (sum <= 0)
return true;
}
// again reset sum to zero
sum = 0;
// Checking possible suffix subarrays.
// If sum of them is less than or equal
// to zero, then return 1
for (int i = n - 1; i >= 0; i--) {
sum += arr[i];
if (sum <= 0)
return true;
}
// Otherwise return 0
return false;
}
// Driver Code
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");
}
}
// This code is contributed by AbhiThakur
Python3
# Python3 program to check if a subarray exists
# with sum greater than the given Array
# Function to check whether there exists
# a subarray whose sum is greater than
# or equal to sum of given array elements
def subarrayPossible(arr, n):
# Initialize sum with 0
sum = 0;
# Checking possible prefix subarrays.
# If sum of them is less than or equal
# to zero, then return 1
for i in range(n):
sum += arr[i];
if (sum <= 0):
return True;
# again reset sum to zero
sum = 0;
# Checking possible suffix subarrays.
# If sum of them is less than or equal
# to zero, then return 1
for i in range(n-1, -1,-1):
sum += arr[i];
if (sum <= 0):
return True;
# Otherwise return 0
return False;
# Driver Code
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");
# This code is contributed by Princi Singh
C#
// C# program to check if a subarray exists
// with sum greater than the given Array
using System;
class GFG{
// Function to check whether there exists
// a subarray whose sum is greater than
// or equal to sum of given array elements
static bool subarrayPossible(int []arr, int n)
{
// Initialize sum with 0
int sum = 0;
// Checking possible prefix subarrays.
// If sum of them is less than or equal
// to zero, then return 1
for (int i = 0; i < n; i++) {
sum += arr[i];
if (sum <= 0)
return true;
}
// again reset sum to zero
sum = 0;
// Checking possible suffix subarrays.
// If sum of them is less than or equal
// to zero, then return 1
for (int i = n - 1; i >= 0; i--) {
sum += arr[i];
if (sum <= 0)
return true;
}
// Otherwise return 0
return false;
}
// Driver Code
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");
}
}
// This code is contributed by Princi Singh
JavaScript
<script>
// JavaScript program to check if a subarray exists
// with sum greater than the given Array
// Function to check whether there exists
// a subarray whose sum is greater than
// or equal to sum of given array elements
function subarrayPossible(arr, n)
{
// Initialize sum with 0
let sum = 0;
// Checking possible prefix subarrays.
// If sum of them is less than or equal
// to zero, then return 1
for (let i = 0; i < n; i++) {
sum += arr[i];
if (sum <= 0)
return true;
}
// again reset sum to zero
sum = 0;
// Checking possible suffix subarrays.
// If sum of them is less than or equal
// to zero, then return 1
for (let i = n - 1; i >= 0; i--) {
sum += arr[i];
if (sum <= 0)
return true;
}
// Otherwise return 0
return false;
}
// Driver Code
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).
Similar Reads
First subarray with negative sum from the given Array Given an array arr[] consisting of N integers, the task is to find the start and end indices of the first subarray with a Negative Sum. Print "-1" if no such subarray exists. Note: In the case of multiple negative-sum subarrays in the given array, the first subarray refers to the subarray with the l
15+ min read
Check if a Subarray exists with sums as a multiple of k Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to the multiple of k, that is, sums up to n*k where n is also an integer. Note: The length of the array won't exceed 10,000. You may assume th
8 min read
Smallest subarray with sum greater than a given value Given an array arr[] of integers and a number x, the task is to find the smallest subarray with a sum strictly greater than x.Examples:Input: x = 51, arr[] = [1, 4, 45, 6, 0, 19]Output: 3Explanation: Minimum length subarray is [4, 45, 6]Input: x = 100, arr[] = [1, 10, 5, 2, 7]Output: 0Explanation: N
15+ min read
Count of elements which is the sum of a subarray of the given Array Given an array arr[], the task is to count elements in an array such that there exists a subarray whose sum is equal to this element.Note: Length of subarray must be greater than 1. Examples: Input: arr[] = {1, 2, 3, 4, 5, 6, 7} Output: 4 Explanation: There are 4 such elements in array - arr[2] = 3
7 min read
Smallest subarray from a given Array with sum greater than or equal to K | Set 2 Given an array A[] consisting of N positive integers and an integer K, the task is to find the length of the smallest subarray with a sum greater than or equal to K. If no such subarray exists, print -1. Examples: Input: arr[] = {3, 1, 7, 1, 2}, K = 11Output: 3Explanation:The smallest subarray with
15+ min read