title: Count Subarrays Equal Number 1S 0S 1
signature: count_subarrays_equal_number_1s_0s_1 <- 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(1L, 6L, 6L, 9L, 9L, 9L, 16L, 18L, 19L, 20L, 21L, 22L, 23L, 26L, 26L, 28L, 39L, 40L, 41L, 43L, 43L, 44L, 44L, 45L, 51L, 51L, 55L, 59L, 60L, 62L, 67L, 67L, 68L, 69L, 70L, 71L, 71L, 72L, 82L, 84L, 88L, 88L, 89L, 89L, 91L, 92L, 92L), 44L, 
          0L
      )))
  
      cat(sprintf("TEST-1...%s\n",driver(
          list(-44L, 74L, -52L, -96L, 46L, 92L, 54L, 56L, -38L, 88L, 40L, 34L, -72L, 8L, 58L, -14L, 36L, 94L, 34L, -90L, -42L, 80L, -12L, -42L, -6L, 78L, -98L, 34L, -88L, -1L, -76L, 90L, 40L, 64L, 26L, 18L, -84L, 72L, 80L), 37L, 
          2L
      )))
  
      cat(sprintf("TEST-2...%s\n",driver(
          list(-1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 32L, 
          14L
      )))
  
      cat(sprintf("TEST-3...%s\n",driver(
          list(60L, 48L, 42L, 95L, 30L, 22L, 80L, 15L, 62L, 38L, 63L, 42L, 39L, 28L, 69L, 71L, 30L, 48L, 67L, 9L, 33L, 74L, 95L, 95L, 72L, 35L, 9L), 18L, 
          0L
      )))
  
      cat(sprintf("TEST-4...%s\n",driver(
          list(-96L, -94L, -94L, -86L, -66L, -66L, -62L, -58L, -36L, -36L, -22L, -18L, -10L, 2L, 4L, 6L, 10L, 16L, 20L, 24L, 26L, 28L, 28L, 28L, 40L, 42L, 44L, 58L, 76L, 78L, 78L, 80L, 90L, 92L), 24L, 
          0L
      )))
  
      cat(sprintf("TEST-5...%s\n",driver(
          list(-1L, -1L, -1L, -1L, -1L, 1L, 1L, 1L, 1L, -1L, 1L, 1L, 1L, 1L, -1L, 1L, -1L, -1L, 1L, 1L, 1L, -1L, 1L, 1L, -1L, 1L, -1L, 1L, 1L, 1L, 0L, 1L, 1L), 28L, 
          32L
      )))
  
      cat(sprintf("TEST-6...%s\n",driver(
          list(4L, 5L, 5L, 6L, 7L, 11L, 16L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 25L, 26L, 27L, 29L, 31L, 34L, 36L, 37L, 40L, 41L, 45L, 45L, 55L, 65L, 69L, 70L, 71L, 71L, 71L, 73L, 73L, 76L, 79L, 80L, 85L, 85L, 88L, 90L, 97L, 98L, 98L, 99L, 99L), 31L, 
          0L
      )))
  
      cat(sprintf("TEST-7...%s\n",driver(
          list(34L, -20L, 38L, -94L, 2L, 32L, -26L, 90L, 94L, -36L, -94L, 6L, -24L, 12L, 4L, 60L, 68L, 64L, -60L, -72L, -54L, -10L, -64L, -48L, -88L, 60L, 48L, 64L, -1L, -26L, 26L, 74L, 32L, -92L, 4L, 84L, 6L, -16L, 30L, -56L, -28L, -86L, -68L), 37L, 
          6L
      )))
  
      cat(sprintf("TEST-8...%s\n",driver(
          list(-1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 19L, 
          7L
      )))
  
      cat(sprintf("TEST-9...%s\n",driver(
          list(4L, 99L, 97L, 63L, 65L, 57L, 69L, 39L, 29L, 91L, 68L, 25L, 84L, 82L, 84L, 89L, 59L, 18L, 77L, 29L, 57L, 40L, 78L, 35L, 23L, 91L, 26L, 71L, 19L, 99L, 12L, 91L, 49L, 71L, 49L, 77L, 67L), 31L, 
          0L
      )))
  
  }
  
  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;
  }