Largest subarray with 0 sum

gfg, potd, java, easy, appris

Problem Statement #

Given an array having both positive and negative integers. The task is to compute the length of the largest subarray with sum 0.

Appris #

Solution #

class GfG
{
    int maxLen(int arr[], int n){
        // Your code here
        Map<Integer, Integer> map = new HashMap<>();
        int sum = 0;
        map.put(sum,0);
        sum+=arr[0];
        
        int res = 0;
        map.put(sum,1);
        
        for(int i=1;i<n;i++){
            sum+=arr[i];
            Integer val = map.get(sum);
            if(val!=null){
                res = Math.max(res,i-val+1);
            }else{
                map.put(sum,i+1);
            }
        }
        
        return res;
    }
}