title: Largest Subarray With Equal Number Of 0S And 1S 1
signature: def 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.test_code: import array
  import bisect
  import collections
  import datetime
  import functools
  import heapq
  import itertools
  import math
  import queue
  import re
  import string
  import sys
  from typing import Any, Dict, List, Optional, Set
  
  
  # SOLUTION CODE
  # ============================================
  PLACEHOLDER_CODE_BODY
  
  # TESTING CODE 
  # ============================================
  def validate_solution(actual, expected):
      return actual == expected
  
  def driver(arr, n, expected):
      try:
          if validate_solution(PLACEHOLDER_FN_NAME(
              arr, n),
              expected
          ):
              return "PASSED"
          return "FAILED"
      except Exception as exception_obj:
          return type(exception_obj).__name__
      
  if __name__ == "__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(f"TEST-0...{result}")
  
      result = driver([1, -66], 1, 0)
      print(f"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)
      print(f"TEST-2...{result}")
  
      result = driver([1, 1, 1, 1, 1, 1, 1, 1, 26, 44, 32], 8, 0)
      print(f"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)
      print(f"TEST-4...{result}")
  
      result = driver([1, 1, 1, 1, 1, 1, 0, 0, 0], 4, 0)
      print(f"TEST-5...{result}")
  
      result = driver([1, 64, 86], 1, 0)
      print(f"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)
      print(f"TEST-7...{result}")
  
      result = driver([0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1], 9, 0)
      print(f"TEST-8...{result}")
  
      result = driver([1, 1, 1, 1, 1, 1, 1, 1, 53], 8, 0)
      print(f"TEST-9...{result}")
  
      sys.exit(0)
question_info.test_case_ids: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
question_info.entry_fn_name: largest_subarray_with_equal_number_of_0s_and_1s_1
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}}]
question_info.entry_cls_name: Solution