title: Largest Subarray With Equal Number Of 0S And 1S 1
signature: function largest_subarray_with_equal_number_of_0s_and_1s_1(arr, n)
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;
  }
question_info.entry_fn_name: largest_subarray_with_equal_number_of_0s_and_1s_1
question_info.entry_cls_name: Solution
question_info.test_case_ids: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
question_info.test_code: 
  
  -- SOLUTION CODE
  -- ============================================
  PLACEHOLDER_CODE_BODY
  
  -- TESTING CODE 
  -- ============================================
  function isCorrect(actual, expected)
      if type(expected) ~= type(actual) then
          return false
      end
      
      if type(expected) == 'table' then
          for k,v_expected in pairs(expected) do
              
              if actual[k] == nil then
                  return false
              end
              local v_actual = actual[k]
              if not isCorrect(v_actual, v_expected) then
                  return false
              end
              actual[k]=nil
          end
      
          if next(actual) then 
              return false
          end
          return true
      else
          return actual == expected
      end
  end
  
  function validateSolution(actual, expected)
      return isCorrect(actual,expected)
  end
  
  function driver(arr, n, expected)
      local exec_result;
      local status, error_msg = pcall(function () 
          exec_result=PLACEHOLDER_FN_NAME(arr, n) 
      end)
      if status then
          if validateSolution(exec_result,expected) then
              return "PASSED"
          end
          return "FAILED"
      else
          return string.format("ERROR=%s",string.gsub(error_msg,".*:[%d]+: ",""))
      end
  end
      
  function main()
      result = driver({1, 1, 1, 1, 1, 1, 1, 1, 1, 42, 60, 66, 69, 70, 70, 73, 74, 80, 99}, 9, 0)
      print(string.format("TEST-0...%s",result))
  
      result = driver({1, -66}, 1, 0)
      print(string.format("TEST-1...%s",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)
      print(string.format("TEST-2...%s",result))
  
      result = driver({1, 1, 1, 1, 1, 1, 1, 1, 26, 44, 32}, 8, 0)
      print(string.format("TEST-3...%s",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)
      print(string.format("TEST-4...%s",result))
  
      result = driver({1, 1, 1, 1, 1, 1, 0, 0, 0}, 4, 0)
      print(string.format("TEST-5...%s",result))
  
      result = driver({1, 64, 86}, 1, 0)
      print(string.format("TEST-6...%s",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)
      print(string.format("TEST-7...%s",result))
  
      result = driver({0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1}, 9, 0)
      print(string.format("TEST-8...%s",result))
  
      result = driver({1, 1, 1, 1, 1, 1, 1, 1, 53}, 8, 0)
      print(string.format("TEST-9...%s",result))
  
  end
  
  main()
question_info.test_list: [{"idx": 0, "outputs": 0, "inputs": {"arr": [1, 1, 1, 1, 1, 1, 1, 1, 1, 42, 60, 66, 69, 70, 70, 73, 74, 80, 99], "n": 9}}, {"idx": 1, "outputs": 0, "inputs": {"arr": [1, -66], "n": 1}}, {"idx": 2, "outputs": 0, "inputs": {"arr": [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], "n": 19}}, {"idx": 3, "outputs": 0, "inputs": {"arr": [1, 1, 1, 1, 1, 1, 1, 1, 26, 44, 32], "n": 8}}, {"idx": 4, "outputs": 0, "inputs": {"arr": [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], "n": 34}}, {"idx": 5, "outputs": 0, "inputs": {"arr": [1, 1, 1, 1, 1, 1, 0, 0, 0], "n": 4}}, {"idx": 6, "outputs": 0, "inputs": {"arr": [1, 64, 86], "n": 1}}, {"idx": 7, "outputs": 0, "inputs": {"arr": [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], "n": 17}}, {"idx": 8, "outputs": 0, "inputs": {"arr": [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1], "n": 9}}, {"idx": 9, "outputs": 0, "inputs": {"arr": [1, 1, 1, 1, 1, 1, 1, 1, 53], "n": 8}}]