title: Count Subarrays Equal Number 1S 0S
signature: count_subarrays_equal_number_1s_0s <- function(arr, n) {
test_code: 
  
  
  # SOLUTION CODE
  # ============================================
  PLACEHOLDER_CODE_BODY
  
  # TESTING CODE 
  # ============================================
  validate_solution <- function(actual, expected) {
      toString(actual)==toString(expected)
  }
  
  driver <- function(arr, n, expected){
      if (validate_solution(PLACEHOLDER_FN_NAME(arr, n), expected)){
          "PASSED"
      } else{
          "FAILED"
      }
  }
  
  main <- function() {
      cat(sprintf("TEST-0...%s\n",driver(
          list(2L, 12L, 18L, 19L, 19L, 20L, 20L, 21L, 25L, 29L, 38L, 54L, 54L, 71L, 72L, 72L, 74L, 75L, 77L, 78L, 80L, 80L, 81L, 84L, 97L, 97L), 24L, 
          0L
      )))
  
      cat(sprintf("TEST-1...%s\n",driver(
          list(10L, 70L, 24L, -38L, 32L, -68L, 88L, -28L, -24L, -70L, -64L, 50L, -30L, 64L, 54L, -6L, 18L, -30L, -30L, -62L, -10L, 94L, -54L, -22L, -88L, 96L, 22L, 26L, -92L, -40L, -76L, 46L, 36L, 30L, 24L, -52L, 0L), 24L, 
          1L
      )))
  
      cat(sprintf("TEST-2...%s\n",driver(
          list(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L), 11L, 
          3L
      )))
  
      cat(sprintf("TEST-3...%s\n",driver(
          list(66L, 50L, 17L, 15L, 86L, 84L, 87L, 24L, 81L, 23L, 71L, 31L, 13L, 72L, 58L, 19L, 29L, 28L, 40L, 14L, 48L, 48L, 81L, 4L, 52L, 88L, 54L, 56L, 10L, 12L, 58L, 55L, 7L, 66L, 15L, 73L, 22L, 2L, 20L, 27L, 57L, 56L, 56L, 31L, 9L, 55L), 40L, 
          0L
      )))
  
      cat(sprintf("TEST-4...%s\n",driver(
          list(-98L, -62L, -60L, 16L, 78L, 82L), 5L, 
          0L
      )))
  
      cat(sprintf("TEST-5...%s\n",driver(
          list(1L, 0L, 1L), 2L, 
          1L
      )))
  
      cat(sprintf("TEST-6...%s\n",driver(
          list(2L, 31L, 34L, 64L), 2L, 
          0L
      )))
  
      cat(sprintf("TEST-7...%s\n",driver(
          list(-70L, 90L, -10L, -64L, -76L, -74L, -12L, -44L, -48L, -54L, 76L, -78L, 8L, 0L, 0L, 78L, -28L, 6L, 98L, -84L, 60L, 60L, 2L, 48L, -96L, -28L, -78L, -76L, -56L, -26L, -60L, 50L, 44L, 34L, -90L, 80L, -30L, -98L, 62L, 36L, -46L, -72L), 25L, 
          2L
      )))
  
      cat(sprintf("TEST-8...%s\n",driver(
          list(1L, 1L, 1L), 1L, 
          0L
      )))
  
      cat(sprintf("TEST-9...%s\n",driver(
          list(37L, 70L, 80L, 61L, 86L, 10L, 17L, 98L, 54L, 89L, 87L, 84L, 11L, 55L, 3L, 52L, 4L, 90L, 98L, 31L, 20L, 62L, 71L, 58L, 58L, 6L, 92L, 5L, 99L, 99L, 72L, 40L, 82L, 54L, 23L, 19L, 85L, 91L, 62L, 98L, 62L, 72L, 93L, 74L), 27L, 
          0L
      )))
  
  }
  
  main()
source_py: def count_subarrays_equal_number_1s_0s(arr, n):
      um = dict()
      curr_sum = 0
      for i in range(n):
          curr_sum += ((- 1) if (arr[i] == 0) else arr[i])
          if um.get(curr_sum):
              um[curr_sum] += 1
          else:
              um[curr_sum] = 1
      count = 0
      for itr in um:
          if (um[itr] > 1):
              count += ((um[itr] * int((um[itr] - 1))) / 2)
      if um.get(0):
          count += um[0]
      return int(count)
source_cpp: int count_subarrays_equal_number_1s_0s(vector<int> arr, int n) {
    unordered_map < int, int > um;
    int curr_sum = 0;
    for ( int i = 0;
    i < n;
    i ++ ) {
      curr_sum += ( arr [ i ] == 0 ) ? - 1 : arr [ i ];
      um [ curr_sum ] ++;
    }
    int count = 0;
    for ( auto itr = um . begin ( );
    itr != um . end ( );
    itr ++ ) {
      if ( itr -> second > 1 ) count += ( ( itr -> second * ( itr -> second - 1 ) ) / 2 );
    }
    if ( um . find ( 0 ) != um . end ( ) ) count += um [ 0 ];
    return count;
  }