Check if a subarray exists with sum greater than the given Array
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 exist 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 |
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 |
Yes
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).
Recommended Posts:
- Check if subarray with given product exists in an array
- Check if product of every pair exists in an array
- Check in binary array the number represented by a subarray is odd or even
- Check whether an Array is Subarray of another Array
- Length of longest subarray in which elements greater than K are more than elements not greater than K
- Smallest subarray with sum greater than a given value
- Largest subarray having sum greater than k
- Smallest subarray such that all elements are greater than K
- Longest subarray in which all elements are greater than K
- Longest Subarray with Sum greater than Equal to Zero
- Longest subarray having average greater than or equal to x | Set-2
- Length of Smallest subarray in range 1 to N with sum greater than a given value
- Longest subarray having average greater than or equal to x
- Length of longest subarray with product greater than or equal to 0
- Count the subarray with sum strictly greater than the sum of remaining elements
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

