Problem:
Find the largest sub-array with 0 sum
Given an array of integers, find length of the largest subarray with sum equals to 0.
For Example:
Input: arr[] = {15, -2, 2, -8, 1, 7, 10, 23};
Output: 5
The largest sub-array with 0 sum is -2, 2, -8, 1, 7
Input: arr[] = {1, 2, 3}
Output: 0
There is no sub-array with 0 sum
Input: arr[] = {1, 0, 3}
Output: 1
Solution:
– Iterate from start to end of array
– And each element in array
– And put current sum with index in map
– – Now more interesting part is – before putting current sum to map check is map contain this sum or not
– – If it contains that means whatever we have add between these two sums is zero and that is what we are looking for
Latest Source Code:
Github: LargestSubArrayWithSumZero.java
Output:
Array : [15, -2, 2, -8, 1, 7, 10, 23] Length (not optimized) : 5 Length (optimized) : 5 Array : [1, 2, 3] Length (not optimized) : 0 Length (optimized) : 0 Array : [1, 0, 3] Length (not optimized) : 1 Length (optimized) : 1