title: Largest Subarray With Equal Number Of 0S And 1S 1
signature: function largestSubarrayWithEqualNumberOf0SAnd1S1(arr, n) {
test_code: 
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE 
  // ============================================
  function convertToString(value) {
      return JSON.stringify(value, function (key, val) {
          if (val instanceof Set) {
              return Array.from(val);
          } return val;});
  }
  
  function validateSolution(actual, expected){
      return convertToString(actual)===convertToString(expected);
  }
  
  function driver(arr, n, expected){
      try{
          if (validateSolution(PLACEHOLDER_FN_NAME(arr, n),expected)){
              return "PASSED";
          }
          return "FAILED";
      } catch (error) {
        return error.name;
      }
      
  }
      
  function main(){
    let result = '';
    result = driver([1, 1, 1, 1, 1, 1, 1, 1, 1, 42, 60, 66, 69, 70, 70, 73, 74, 80, 99], 9, 0);
    console.log(`TEST-0...${result}`);
  
    result = driver([1, -66], 1, 0);
    console.log(`TEST-1...${result}`);
  
    result = driver([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);
    console.log(`TEST-2...${result}`);
  
    result = driver([1, 1, 1, 1, 1, 1, 1, 1, 26, 44, 32], 8, 0);
    console.log(`TEST-3...${result}`);
  
    result = driver([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);
    console.log(`TEST-4...${result}`);
  
    result = driver([1, 1, 1, 1, 1, 1, 0, 0, 0], 4, 0);
    console.log(`TEST-5...${result}`);
  
    result = driver([1, 64, 86], 1, 0);
    console.log(`TEST-6...${result}`);
  
    result = driver([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);
    console.log(`TEST-7...${result}`);
  
    result = driver([0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1], 9, 0);
    console.log(`TEST-8...${result}`);
  
    result = driver([1, 1, 1, 1, 1, 1, 1, 1, 53], 8, 0);
    console.log(`TEST-9...${result}`);
  
    process.exit(0);
  };
  
  if (require.main === module) {
    main();
  }
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;
  }