title: Count Subarrays With Same Even And Odd Elements
signature: class Solution {
  
      public Integer countSubarraysWithSameEvenAndOddElements(ArrayList<Integer> arr, Integer n) {
source_py: def count_subarrays_with_same_even_and_odd_elements(arr, n):
      difference = 0
      ans = 0
      hash_positive = ([0] * (n + 1))
      hash_negative = ([0] * (n + 1))
      hash_positive[0] = 1
      for i in range(n):
          if ((arr[i] & 1) == 1):
              difference = (difference + 1)
          else:
              difference = (difference - 1)
          if (difference < 0):
              ans += hash_negative[(- difference)]
              hash_negative[(- difference)] = (hash_negative[(- difference)] + 1)
          else:
              ans += hash_positive[difference]
              hash_positive[difference] = (hash_positive[difference] + 1)
      return ans
source_cpp: int count_subarrays_with_same_even_and_odd_elements(vector<int> arr, int n) {
    int difference = 0;
    int ans = 0;
    int hash_positive [ n + 1 ], hash_negative [ n + 1 ];
    fill_n ( hash_positive, n + 1, 0 );
    fill_n ( hash_negative, n + 1, 0 );
    hash_positive [ 0 ] = 1;
    for ( int i = 0;
    i < n;
    i ++ ) {
      if ( arr [ i ] & 1 == 1 ) difference ++;
      else difference --;
      if ( difference < 0 ) {
        ans += hash_negative [ - difference ];
        hash_negative [ - difference ] ++;
      }
      else {
        ans += hash_positive [ difference ];
        hash_positive [ difference ] ++;
      }
    }
    return ans;
  }
question_info.extension: java
question_info.entry_fn_name: countSubarraysWithSameEvenAndOddElements
question_info.entry_cls_name: Solution
question_info.test_list: [{"idx": 0, "outputs": 50, "inputs": {"arr": [7, 8, 12, 13, 14, 19, 20, 22, 28, 30, 31, 31, 32, 34, 34, 39, 39, 43, 45, 46, 47, 62, 63, 63, 65, 66, 69, 69, 71, 76, 79, 82, 83, 88, 89, 92, 93, 95, 97, 97], "n": 26}}, {"idx": 1, "outputs": 0, "inputs": {"arr": [20, -98, -44, -82, 28, 20, -76, -16, 42, 0, -88, 74, 56, 6, -68, -30, 28, 88, 58, -78, 46, 2], "n": 15}}, {"idx": 2, "outputs": 11, "inputs": {"arr": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], "n": 24}}, {"idx": 3, "outputs": 43, "inputs": {"arr": [44, 4, 29, 83, 28, 75, 58, 89, 40, 38, 29, 45, 21, 87, 97, 42, 95, 20, 48, 38, 15, 67, 23, 81, 50, 53, 64, 67, 30, 13, 52, 56, 87, 10, 80, 38, 31, 19], "n": 23}}, {"idx": 4, "outputs": 0, "inputs": {"arr": [-94, -94, -94, -90, -88, -86, -86, -82, -78, -76, -74, -68, -64, -62, -62, -60, -52, -48, -48, -46, -44, -44, -32, -28, -22, -12, -12, -8, -8, -4, 4, 6, 10, 14, 28, 40, 42, 52, 56, 60, 60, 60, 64, 68, 70, 82, 82, 84, 96], "n": 48}}, {"idx": 5, "outputs": 68, "inputs": {"arr": [1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1], "n": 27}}, {"idx": 6, "outputs": 5, "inputs": {"arr": [6, 16, 21, 26, 34, 35, 44, 46, 46, 86], "n": 7}}, {"idx": 7, "outputs": 0, "inputs": {"arr": [86, 12, 80, 46, -12, 6], "n": 4}}, {"idx": 8, "outputs": 4, "inputs": {"arr": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], "n": 15}}, {"idx": 9, "outputs": 7, "inputs": {"arr": [71, 94, 91, 19, 85, 5, 87, 96, 66, 17, 95, 5, 32, 17, 93, 48, 46, 24], "n": 12}}]
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(7, 8, 12, 13, 14, 19, 20, 22, 28, 30, 31, 31, 32, 34, 34, 39, 39, 43, 45, 46, 47, 62, 63, 63, 65, 66, 69, 69, 71, 76, 79, 82, 83, 88, 89, 92, 93, 95, 97, 97)), 26, 50);
          System.out.println(String.format("%s%s","TEST-0...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(20, -98, -44, -82, 28, 20, -76, -16, 42, 0, -88, 74, 56, 6, -68, -30, 28, 88, 58, -78, 46, 2)), 15, 0);
          System.out.println(String.format("%s%s","TEST-1...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), 24, 11);
          System.out.println(String.format("%s%s","TEST-2...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(44, 4, 29, 83, 28, 75, 58, 89, 40, 38, 29, 45, 21, 87, 97, 42, 95, 20, 48, 38, 15, 67, 23, 81, 50, 53, 64, 67, 30, 13, 52, 56, 87, 10, 80, 38, 31, 19)), 23, 43);
          System.out.println(String.format("%s%s","TEST-3...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(-94, -94, -94, -90, -88, -86, -86, -82, -78, -76, -74, -68, -64, -62, -62, -60, -52, -48, -48, -46, -44, -44, -32, -28, -22, -12, -12, -8, -8, -4, 4, 6, 10, 14, 28, 40, 42, 52, 56, 60, 60, 60, 64, 68, 70, 82, 82, 84, 96)), 48, 0);
          System.out.println(String.format("%s%s","TEST-4...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1)), 27, 68);
          System.out.println(String.format("%s%s","TEST-5...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(6, 16, 21, 26, 34, 35, 44, 46, 46, 86)), 7, 5);
          System.out.println(String.format("%s%s","TEST-6...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(86, 12, 80, 46, -12, 6)), 4, 0);
          System.out.println(String.format("%s%s","TEST-7...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), 15, 4);
          System.out.println(String.format("%s%s","TEST-8...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(71, 94, 91, 19, 85, 5, 87, 96, 66, 17, 95, 5, 32, 17, 93, 48, 46, 24)), 12, 7);
          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__