title: Count Subarrays Equal Number 1S 0S 1
signature: function countSubarraysEqualNumber1S0S1(arr: Array<number>, n: number): number {
test_code: 
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE 
  // ============================================
  function convertToString(value: any): string{
      return JSON.stringify(value, function (key, val) {
          if (val instanceof Set) {
              return Array.from(val);
          } return val;});
  }
  
  function validateSolution(actual: number, expected: number): boolean {
      return convertToString(actual) === convertToString(expected);
  }
  
  function driver(arr: Array<number>, n: number, expected:number): string {
      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, 6, 6, 9, 9, 9, 16, 18, 19, 20, 21, 22, 23, 26, 26, 28, 39, 40, 41, 43, 43, 44, 44, 45, 51, 51, 55, 59, 60, 62, 67, 67, 68, 69, 70, 71, 71, 72, 82, 84, 88, 88, 89, 89, 91, 92, 92], 44, 0);
    console.log(`TEST-0...${result}`);
  
    result = driver([-44, 74, -52, -96, 46, 92, 54, 56, -38, 88, 40, 34, -72, 8, 58, -14, 36, 94, 34, -90, -42, 80, -12, -42, -6, 78, -98, 34, -88, -1, -76, 90, 40, 64, 26, 18, -84, 72, 80], 37, 2);
    console.log(`TEST-1...${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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], 32, 14);
    console.log(`TEST-2...${result}`);
  
    result = driver([60, 48, 42, 95, 30, 22, 80, 15, 62, 38, 63, 42, 39, 28, 69, 71, 30, 48, 67, 9, 33, 74, 95, 95, 72, 35, 9], 18, 0);
    console.log(`TEST-3...${result}`);
  
    result = driver([-96, -94, -94, -86, -66, -66, -62, -58, -36, -36, -22, -18, -10, 2, 4, 6, 10, 16, 20, 24, 26, 28, 28, 28, 40, 42, 44, 58, 76, 78, 78, 80, 90, 92], 24, 0);
    console.log(`TEST-4...${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, 0, 1, 1], 28, 32);
    console.log(`TEST-5...${result}`);
  
    result = driver([4, 5, 5, 6, 7, 11, 16, 16, 17, 18, 19, 20, 21, 22, 23, 25, 26, 27, 29, 31, 34, 36, 37, 40, 41, 45, 45, 55, 65, 69, 70, 71, 71, 71, 73, 73, 76, 79, 80, 85, 85, 88, 90, 97, 98, 98, 99, 99], 31, 0);
    console.log(`TEST-6...${result}`);
  
    result = driver([34, -20, 38, -94, 2, 32, -26, 90, 94, -36, -94, 6, -24, 12, 4, 60, 68, 64, -60, -72, -54, -10, -64, -48, -88, 60, 48, 64, -1, -26, 26, 74, 32, -92, 4, 84, 6, -16, 30, -56, -28, -86, -68], 37, 6);
    console.log(`TEST-7...${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], 19, 7);
    console.log(`TEST-8...${result}`);
  
    result = driver([4, 99, 97, 63, 65, 57, 69, 39, 29, 91, 68, 25, 84, 82, 84, 89, 59, 18, 77, 29, 57, 40, 78, 35, 23, 91, 26, 71, 19, 99, 12, 91, 49, 71, 49, 77, 67], 31, 0);
    console.log(`TEST-9...${result}`);
  
  };
  
  main();
source_py: def count_subarrays_equal_number_1s_0s_1(arr, n):
      mp = dict()
      Sum = 0
      count = 0
      for i in range(n):
          if (arr[i] == 0):
              arr[i] = (- 1)
          Sum += arr[i]
          if (Sum == 0):
              count += 1
          if (Sum in mp.keys()):
              count += mp[Sum]
          mp[Sum] = (mp.get(Sum, 0) + 1)
      return count
source_cpp: int count_subarrays_equal_number_1s_0s_1(vector<int> arr, int n) {
    map < int, int > mp;
    int sum = 0;
    int count = 0;
    for ( int i = 0;
    i < n;
    i ++ ) {
      if ( arr [ i ] == 0 ) arr [ i ] = - 1;
      sum += arr [ i ];
      if ( sum == 0 ) count ++;
      if ( mp [ sum ] ) count += mp [ sum ];
      if ( mp [ sum ] == 0 ) mp [ sum ] = 1;
      else mp [ sum ] ++;
    }
    return count;
  }