title: Largest Subarray With Equal Number Of 0S And 1S 1
signature: class Solution {
  
      public Integer largestSubarrayWithEqualNumberOf0SAnd1S1(ArrayList<Integer> arr, Integer n) {
test_code: import static java.util.Map.entry;  
  import java.util.*;
  import java.lang.*;
  // TESTING CODE 
  // ============================================
  class QuestionEvaluator {
      public static String validateSolution(ArrayList<Integer> arr, Integer n, Integer expected){
          try{
              Driver driver = new Driver();
              if (driver.run(arr, n,expected)){
                  return "PASSED";
              }
              return "FAILED";
              
          } catch (Exception exception_obj){
              return exception_obj.getClass().getSimpleName();
          }
  
      }
      public static void main(String[] args) {
          String result;
          result = validateSolution(new ArrayList<>(Arrays.asList(1, 1, 1, 1, 1, 1, 1, 1, 1, 42, 60, 66, 69, 70, 70, 73, 74, 80, 99)), 9, 0);
          System.out.println(String.format("%s%s","TEST-0...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(1, -66)), 1, 0);
          System.out.println(String.format("%s%s","TEST-1...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), 19, 0);
          System.out.println(String.format("%s%s","TEST-2...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(1, 1, 1, 1, 1, 1, 1, 1, 26, 44, 32)), 8, 0);
          System.out.println(String.format("%s%s","TEST-3...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 72, 74, 86, 92, 94)), 34, 0);
          System.out.println(String.format("%s%s","TEST-4...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(1, 1, 1, 1, 1, 1, 0, 0, 0)), 4, 0);
          System.out.println(String.format("%s%s","TEST-5...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(1, 64, 86)), 1, 0);
          System.out.println(String.format("%s%s","TEST-6...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, -68, 12, -44, 62, 60, -34, -52, 18)), 17, 0);
          System.out.println(String.format("%s%s","TEST-7...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1)), 9, 0);
          System.out.println(String.format("%s%s","TEST-8...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(1, 1, 1, 1, 1, 1, 1, 1, 53)), 8, 0);
          System.out.println(String.format("%s%s","TEST-9...",result));    
      
      }
  }
  
  class Driver{
      public boolean run(ArrayList<Integer> arr, Integer n, Integer expected){
  
          PLACEHOLDER_CLS_NAME sol = new PLACEHOLDER_CLS_NAME();
          return validateSolution(sol.PLACEHOLDER_FN_NAME(arr, n),expected);
      }
      
      public boolean validateSolution(Integer actual, Integer expected){
          return actual.equals(expected);
      }
  }
  
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
source_py: def largest_subarray_with_equal_number_of_0s_and_1s_1(arr, n):
      hash_map = {
          
      }
      curr_sum = 0
      max_len = 0
      ending_index = (- 1)
      for i in range(0, n):
          if (arr[i] == 0):
              arr[i] = (- 1)
          else:
              arr[i] = 1
      for i in range(0, n):
          curr_sum = (curr_sum + arr[i])
          if (curr_sum == 0):
              max_len = (i + 1)
              ending_index = i
          if ((curr_sum + n) in hash_map):
              max_len = max(max_len, (i - hash_map[(curr_sum + n)]))
          else:
              hash_map[curr_sum] = i
      for i in range(0, n):
          if (arr[i] == (- 1)):
              arr[i] = 0
          else:
              arr[i] = 1
      print(((ending_index - max_len) + 1), end=' ')
      print('to', end=' ')
      print(ending_index)
      return max_len
source_cpp: int largest_subarray_with_equal_number_of_0s_and_1s_1(vector<int> arr, int n) {
    unordered_map < int, int > hM;
    int sum = 0;
    int max_len = 0;
    int ending_index = - 1;
    for ( int i = 0;
    i < n;
    i ++ ) arr [ i ] = ( arr [ i ] == 0 ) ? - 1 : 1;
    for ( int i = 0;
    i < n;
    i ++ ) {
      sum += arr [ i ];
      if ( sum == 0 ) {
        max_len = i + 1;
        ending_index = i;
      }
      if ( hM . find ( sum + n ) != hM . end ( ) ) {
        if ( max_len < i - hM [ sum + n ] ) {
          max_len = i - hM [ sum + n ];
          ending_index = i;
        }
      }
      else hM [ sum + n ] = i;
    }
    for ( int i = 0;
    i < n;
    i ++ ) arr [ i ] = ( arr [ i ] == - 1 ) ? 0 : 1;
    return max_len;
  }