Given an array, print all subarrays in the array which has sum 0.
Input: arr = [6, 3, -1, -3, 4, -2, 2, 4, 6, -12, -7] Output: Subarray found from Index 2 to 4 Subarray found from Index 2 to 6 Subarray found from Index 5 to 6 Subarray found from Index 6 to 9 Subarray found from Index 0 to 10
Related posts: Find if there is a subarray with 0 sum
A simple solution is to consider all subarrays one by one and check if sum of every subarray is equal to 0 or not. The complexity of this solution would be O(n^2).
A better approach is to use Hashing.
Do following for each element in the array
- Maintain sum of elements encountered so far in a variable (say sum).
- If current sum is 0, we found a subarray starting from index 0 and ending at index current index
- Check if current sum exists in the hash table or not.
- If current sum already exists in the hash table then it indicates that this sum was the sum of some sub-array elements arr…arr[i] and now the same sum is obtained for the current sub-array arr…arr[j] which means that the sum of the sub-array arr[i+1]…arr[j] must be 0.
- Insert current sum into the hash table
Below is a dry run of the above approach:
Below is the implementation of the above approach:
Subarray found from Index 2 to 4 Subarray found from Index 2 to 6 Subarray found from Index 5 to 6 Subarray found from Index 6 to 9 Subarray found from Index 0 to 10
This article is contributed by Aditya Goel. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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.
- Differences between number of increasing subarrays and decreasing subarrays in k sized windows
- Sum of minimum and maximum elements of all subarrays of size k.
- Sum of all Subarrays | Set 1
- Count all subarrays whose sum can be split as difference of squares of two Integers
- Generate Array whose sum of all K-size subarrays divided by N leaves remainder X
- Sum of bitwise OR of all subarrays
- Sum of minimum elements of all subarrays
- Sum of Bitwise-OR of all subarrays of a given Array | Set 2
- Sum of XOR of all subarrays
- Sum of minimum element of all subarrays of a sorted array
- Sum of maximum of all subarrays | Divide and Conquer
- Calculate the Sum of GCD over all subarrays
- Maximum length L such that the sum of all subarrays of length L is less than K
- Sum of all subarrays of size K
- Make sum of all subarrays of length K equal by only inserting elements
- Generate a unique Array of length N with sum of all subarrays divisible by N
- Sum of all differences between Maximum and Minimum of increasing Subarrays
- Minimize the Sum of all the subarrays made up of the products of same-indexed elements
- Maximum subarray size, such that all subarrays of that size have sum less than k
- Sum of all odd length subarrays