title: Largest Subarray With Equal Number Of 0S And 1S 1
signature: class Solution {
  
      public int LargestSubarrayWithEqualNumberOf0SAnd1S1(List<int> arr, int n) {
test_code: using System;
  using System.Numerics;
  using System.Diagnostics;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Security.Cryptography;
  using System.Web.UI;
  using System.Web.Script.Serialization;
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE 
  // ============================================
  namespace EvaluatePrediction {
      class TestRunner {
          public bool ValidateSolution(int actual, int expected){
              return new JavaScriptSerializer().Serialize(actual) == new JavaScriptSerializer().Serialize(expected);
          }
  
          public string driver(List<int> arr, int n, int expected){
              try{
                PLACEHOLDER_CLS_NAME solution_cls = new PLACEHOLDER_CLS_NAME();
                int pred_result = solution_cls.PLACEHOLDER_FN_NAME(arr, n); 
                if (ValidateSolution(
                      pred_result,
                      expected)){
                  return "PASSED";
                }
                return "FAILED";
              } 
              catch (Exception exception_obj)
              {
                return exception_obj.GetType().FullName;
              }
          }
          static void Main(string[] args) {
              TestRunner runner = new TestRunner();
              String result;
              result = runner.driver(
                  new List<int>{1, 1, 1, 1, 1, 1, 1, 1, 1, 42, 60, 66, 69, 70, 70, 73, 74, 80, 99}, 9, 
                  0
              );
              Console.WriteLine(string.Format(
                  "TEST-0...{0}",
                  result)
              );result = runner.driver(
                  new List<int>{1, -66}, 1, 
                  0
              );
              Console.WriteLine(string.Format(
                  "TEST-1...{0}",
                  result)
              );result = runner.driver(
                  new List<int>{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.WriteLine(string.Format(
                  "TEST-2...{0}",
                  result)
              );result = runner.driver(
                  new List<int>{1, 1, 1, 1, 1, 1, 1, 1, 26, 44, 32}, 8, 
                  0
              );
              Console.WriteLine(string.Format(
                  "TEST-3...{0}",
                  result)
              );result = runner.driver(
                  new List<int>{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.WriteLine(string.Format(
                  "TEST-4...{0}",
                  result)
              );result = runner.driver(
                  new List<int>{1, 1, 1, 1, 1, 1, 0, 0, 0}, 4, 
                  0
              );
              Console.WriteLine(string.Format(
                  "TEST-5...{0}",
                  result)
              );result = runner.driver(
                  new List<int>{1, 64, 86}, 1, 
                  0
              );
              Console.WriteLine(string.Format(
                  "TEST-6...{0}",
                  result)
              );result = runner.driver(
                  new List<int>{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.WriteLine(string.Format(
                  "TEST-7...{0}",
                  result)
              );result = runner.driver(
                  new List<int>{0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1}, 9, 
                  0
              );
              Console.WriteLine(string.Format(
                  "TEST-8...{0}",
                  result)
              );result = runner.driver(
                  new List<int>{1, 1, 1, 1, 1, 1, 1, 1, 53}, 8, 
                  0
              );
              Console.WriteLine(string.Format(
                  "TEST-9...{0}",
                  result)
              );
          }
      }
  }
  
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;
  }