title: Count Subarrays Equal Number 1S 0S
signature: func countSubarraysEqualNumber1S0S(arr []int, n int) int {
test_code: package main
  
  import (
      "fmt"
      "reflect"
      "math"
      "sort"
  )
  
  // SOLUTION CODE
  // ===================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE
  // ===================================
  func validateSolution(actual int, expected int) bool {
      return reflect.DeepEqual(actual,expected)
  }
  
  func driver(arr []int, n int, expected int) string {
      if validateSolution(PLACEHOLDER_FN_NAME(arr, n),expected) {
              return "PASSED"
      }
      return "FAILED"
  
  }
  
  func main() {
  
      // Use imports to avoid undefined errors
      var _ = reflect.DeepEqual
      _ = math.Abs
      _ = sort.Sort
      result := ""
      result = driver([]int{2, 12, 18, 19, 19, 20, 20, 21, 25, 29, 38, 54, 54, 71, 72, 72, 74, 75, 77, 78, 80, 80, 81, 84, 97, 97}, 
  	24, 0)
      fmt.Printf("TEST-0...%s\n", result)
      
      result = driver([]int{10, 70, 24, -38, 32, -68, 88, -28, -24, -70, -64, 50, -30, 64, 54, -6, 18, -30, -30, -62, -10, 94, -54, -22, -88, 96, 22, 26, -92, -40, -76, 46, 36, 30, 24, -52, 0}, 
  	24, 1)
      fmt.Printf("TEST-1...%s\n", result)
      
      result = driver([]int{0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1}, 
  	11, 3)
      fmt.Printf("TEST-2...%s\n", result)
      
      result = driver([]int{66, 50, 17, 15, 86, 84, 87, 24, 81, 23, 71, 31, 13, 72, 58, 19, 29, 28, 40, 14, 48, 48, 81, 4, 52, 88, 54, 56, 10, 12, 58, 55, 7, 66, 15, 73, 22, 2, 20, 27, 57, 56, 56, 31, 9, 55}, 
  	40, 0)
      fmt.Printf("TEST-3...%s\n", result)
      
      result = driver([]int{-98, -62, -60, 16, 78, 82}, 
  	5, 0)
      fmt.Printf("TEST-4...%s\n", result)
      
      result = driver([]int{1, 0, 1}, 
  	2, 1)
      fmt.Printf("TEST-5...%s\n", result)
      
      result = driver([]int{2, 31, 34, 64}, 
  	2, 0)
      fmt.Printf("TEST-6...%s\n", result)
      
      result = driver([]int{-70, 90, -10, -64, -76, -74, -12, -44, -48, -54, 76, -78, 8, 0, 0, 78, -28, 6, 98, -84, 60, 60, 2, 48, -96, -28, -78, -76, -56, -26, -60, 50, 44, 34, -90, 80, -30, -98, 62, 36, -46, -72}, 
  	25, 2)
      fmt.Printf("TEST-7...%s\n", result)
      
      result = driver([]int{1, 1, 1}, 
  	1, 0)
      fmt.Printf("TEST-8...%s\n", result)
      
      result = driver([]int{37, 70, 80, 61, 86, 10, 17, 98, 54, 89, 87, 84, 11, 55, 3, 52, 4, 90, 98, 31, 20, 62, 71, 58, 58, 6, 92, 5, 99, 99, 72, 40, 82, 54, 23, 19, 85, 91, 62, 98, 62, 72, 93, 74}, 
  	27, 0)
      fmt.Printf("TEST-9...%s\n", result)
      
          
  }
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;
  }