# 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.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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++ program to check if a subarray exists ` `// with sum greater than the given Array ` `#include ` `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 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 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# 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 `

Output:
```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).

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

Interns at Geeksforgeeks

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.

Article Tags :
Practice Tags :