title: Count Subarrays With Same Even And Odd Elements
signature: function countSubarraysWithSameEvenAndOddElements($arr, $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;
  }