Largest subarray of 0's and 1's | Practice | GeeksforGeeks

Given an array of 0s and 1s. Find the length of the largest subarray with equal number of 0s and 1s.
Example 1:
Input:
N = 4
A[] = {0,1,0,1}
Output: 4
Explanation: The array from index [0...3]
contains equal number of 0's and 1's.
Thus maximum length of subarray having
equal number of 0's and 1's is 4.

Example 2:
Input:
N = 5
A[] = {0,0,1,0,0}
Output: 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxLen() which takes the array arr[] and the size of the array as inputs and returns the length of the largest subarray with equal number of 0s and 1s.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 <= N <= 105
0 <= A[] <= 1
0
harshpandeyalfa23 weeks ago
C++
(0.37sec)
int maxLen(int arr[], int N)   {       for(int i=0;i<N;i++)         if(arr[i]==0)          arr[i]=-1;       int ps=0;       int res=0;       unordered_map<int,int> s;       for(int i=0;i<N;i++)       {           ps=ps+arr[i];           if(ps==0)             res=i+1;           else if(s.find(ps) ==s.end())            s.insert({ps,i});           else if(s.find(ps)!=s.end())            res=max(res,i-s[ps]);       }       return res;   }
0
pranjalmaurya20121361 month ago
What are the wrong code please tell me
 
 
 int sum=0;              for(int i=0;i<N;i++){           int co=0, c1=0;                      for(int j=i;j<N;j++){              if( arr[j]==0){                                      co++;              }                                        else{                  c1++;              }              if(co==c1){                  sum=Max(res,j-i+1);              }                         }       }       return sum;
 
 
0
sknwd88643 months ago
int maxLen(int arr[], int n)
    {
        for(int i = 0; i < n; i++)
        {
            if(arr[i] == 0)
            {
                arr[i] = -1;
            }
        }
        
        unordered_map<int, int> mp;
        
        int longest = 0;
        
        mp[0] = -1;
        
        int sum = 0;
        
        for(int i = 0; i < n; i++)
        {
            sum += arr[i];
            
            if(mp.count(sum))
            {
                longest = max(longest, i - mp[sum]);
            }
            else
            {
                mp[sum] = i;
            }
        }
        
        return longest;
    }
0
priyeshanand93 months ago
class Solution{ public:   int maxLen(int arr[], int N)   {       for(int i=0; i<N; i++){           if(arr[i]==0)           arr[i]=-1;       }       map<int,int>m;       m[arr[0]]=0;;       int l=0;       for(int i=1; i<N; i++){           arr[i]=arr[i-1]+arr[i];           if(arr[i]==0)           l=max(l,i+1);           else if(m.count(arr[i])>0)           l=max(l,i-m[arr[i]]);           else m[arr[i]]=i;       }       return l;   }};
0
kurianspaul3 months ago
Clear and concise solution!
int maxLen(int[] arr, int N)
   {
       Map<Integer,Integer> map = new HashMap<>();
       map.put(0,-1);
       int res = 0,cumSum=0;
       for(int i=0;i<arr.length;++i) {
           cumSum = arr[i]==1 ? cumSum+1 : cumSum-1;
           if(map.containsKey(cumSum)) res = Math.max(res,i-map.get(cumSum));
           else map.put(cumSum,i);
       }
       return res;
   }
+1
nikhilchakravarthy093 months ago
int maxLen(int arr[], int N)
   {
       int sum=0;
       int ans=0;
       unordered_map<int,int> mp;
       for(int i=0;i<N;i++){
           if(arr[i]==0)
               sum-=1;
           else
               sum+=1;
           if(sum==0)
               ans=max(ans,i+1);
           if(mp.find(sum) != mp.end())
               ans=max(ans,i-mp[sum]);
           else
               mp[sum]=i;
       }
       return ans;
   }
-1
aloksinghbais023 months ago
C++ solution having time complexity as O(N) and space complexity as O(N) is as follows :-
 
Note :- Similar question in which we try to find largest subarray with given sum k (In this question k is 0).
 
Link for other similar question :- 
 
https://practice.geeksforgeeks.org/problems/longest-sub-array-with-sum-k0809/1#
 
Execution Time :- 0.4 / 1.6 sec
 
int maxLen(int arr[], int N){       unordered_map<int,int> firstLastInd;       unordered_map<int,bool> mp;       int len = 0;       int sum = 0; mp[0] = true, firstLastInd[0] = 0;       for(int i=0; i<N; i++){           sum += (arr[i] == 0 ? -1 : 1);           if(mp[sum]){               len = max(len,i - firstLastInd[sum] + 1);           }                      mp[sum] = true;           if(firstLastInd[sum] == 0 && sum)                                      firstLastInd[sum] = i+1;       }              return (len);   }
0
raunakmishra12433 months ago
int maxLen(int arr[], int N)
    {
        unordered_map<int,int>m;
        
        
        for(int i=0;i<N;i++)
        {
            if(arr[i]==0)
            arr[i]=-1;
        }
        int sum=0;
        m[sum]=-1;
        int ans=INT_MIN;
        for(int i=0;i<N;i++)
        {
            sum=sum+arr[i];
            if(m.find(sum)!=m.end())
            {
                
                ans=max(ans,abs(m[sum]-i));
                
            }
            else
            m[sum]=i;
            
        }
        if(ans==INT_MIN)
        return 0;
        return ans;
        
    }
};
0
radheshyamnitj4 months ago
int length=0;
       map<int,int>m;
       
       int sum=0;
       m[0]=-1;
       
       for(int i=0;i<N;i++){
           if(arr[i]==0)
            sum+=-1;
            else
            sum+=1;
            
            if(m.find(sum)==m.end())
            m[sum]=i;
            
            else
            {
                length=max(length,i-m[sum]);
            }
       }
       return length;
+8
amanshakya0074 months ago
HINT 1. Think this question same as largest subarray with zero sum2. to make it  zero sum you have to do something 3. make all 0 to -1
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.