title: Count Subarrays Equal Number 1S 0S
signature: class Solution {
  
      public Integer countSubarraysEqualNumber1S0S(ArrayList<Integer> arr, Integer n) {
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;
  }
question_info.extension: java
question_info.entry_fn_name: countSubarraysEqualNumber1S0S
question_info.entry_cls_name: Solution
question_info.test_list: [{"idx": 0, "outputs": 0, "inputs": {"arr": [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], "n": 24}}, {"idx": 1, "outputs": 1, "inputs": {"arr": [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], "n": 24}}, {"idx": 2, "outputs": 3, "inputs": {"arr": [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1], "n": 11}}, {"idx": 3, "outputs": 0, "inputs": {"arr": [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], "n": 40}}, {"idx": 4, "outputs": 0, "inputs": {"arr": [-98, -62, -60, 16, 78, 82], "n": 5}}, {"idx": 5, "outputs": 1, "inputs": {"arr": [1, 0, 1], "n": 2}}, {"idx": 6, "outputs": 0, "inputs": {"arr": [2, 31, 34, 64], "n": 2}}, {"idx": 7, "outputs": 2, "inputs": {"arr": [-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], "n": 25}}, {"idx": 8, "outputs": 0, "inputs": {"arr": [1, 1, 1], "n": 1}}, {"idx": 9, "outputs": 0, "inputs": {"arr": [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], "n": 27}}]
question_info.test_code: import static java.util.Map.entry;  
  import java.util.*;
  import java.lang.*;
  // TESTING CODE 
  // ============================================
  class QuestionEvaluator {
      public static String validateSolution(ArrayList<Integer> arr, Integer n, Integer expected){
          try{
              Driver driver = new Driver();
              if (driver.run(arr, n,expected)){
                  return "PASSED";
              }
              return "FAILED";
              
          } catch (Exception exception_obj){
              return exception_obj.getClass().getSimpleName();
          }
  
      }
      public static void main(String[] args) {
          String result;
          result = validateSolution(new ArrayList<>(Arrays.asList(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);
          System.out.println(String.format("%s%s","TEST-0...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(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);
          System.out.println(String.format("%s%s","TEST-1...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1)), 11, 3);
          System.out.println(String.format("%s%s","TEST-2...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(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);
          System.out.println(String.format("%s%s","TEST-3...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(-98, -62, -60, 16, 78, 82)), 5, 0);
          System.out.println(String.format("%s%s","TEST-4...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(1, 0, 1)), 2, 1);
          System.out.println(String.format("%s%s","TEST-5...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(2, 31, 34, 64)), 2, 0);
          System.out.println(String.format("%s%s","TEST-6...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(-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);
          System.out.println(String.format("%s%s","TEST-7...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(1, 1, 1)), 1, 0);
          System.out.println(String.format("%s%s","TEST-8...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(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);
          System.out.println(String.format("%s%s","TEST-9...",result));    
      
      }
  }
  
  class Driver{
      public boolean run(ArrayList<Integer> arr, Integer n, Integer expected){
  
          PLACEHOLDER_CLS_NAME sol = new PLACEHOLDER_CLS_NAME();
          return validateSolution(sol.PLACEHOLDER_FN_NAME(arr, n),expected);
      }
      
      public boolean validateSolution(Integer actual, Integer expected){
          return actual.equals(expected);
      }
  }
  
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
question_info.test_case_ids: 0 1 2 3 4 5 6 7 8 9
question_info.commands: java __FILENAME__