title: Count Subarrays With Same Even And Odd Elements
signature: int countSubarraysWithSameEvenAndOddElements(vector<int> arr, int n) {
test_code: #include <algorithm>
  #include <assert.h>
  #include <cstdlib>
  #include <iostream>
  #include <map>
  #include <set>
  #include <string>
  #include <tuple> 
  #include <unordered_map>
  #include <vector>
  #include <math.h>
  #include <cmath>
  #include <bits/stdc++.h>
  #include <numeric>
  #include <functional>
  
  using namespace std;
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE 
  // ============================================
  bool validateSolution(int actual, int expected){
      return actual == expected;
  }
  
  string driver(vector<int> arr, int n, int expected){
      try
      {
          if (validateSolution(PLACEHOLDER_FN_NAME(arr, n), expected)){
              return "PASSED";
          }
          return "FAILED";
      }
      catch (const std::overflow_error& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (const std::runtime_error& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (const std::exception& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (...)
      {
          return "UNK_ERROR";
      }
  }
  
  int main() {
      string result = "";
  
      result = driver({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); 
      cout << "TEST-" << 0 << "..."
           <<  result
           << "\n";
  
      result = driver({20, -98, -44, -82, 28, 20, -76, -16, 42, 0, -88, 74, 56, 6, -68, -30, 28, 88, 58, -78, 46, 2}, 15, 0); 
      cout << "TEST-" << 1 << "..."
           <<  result
           << "\n";
  
      result = driver({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); 
      cout << "TEST-" << 2 << "..."
           <<  result
           << "\n";
  
      result = driver({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); 
      cout << "TEST-" << 3 << "..."
           <<  result
           << "\n";
  
      result = driver({-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); 
      cout << "TEST-" << 4 << "..."
           <<  result
           << "\n";
  
      result = driver({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); 
      cout << "TEST-" << 5 << "..."
           <<  result
           << "\n";
  
      result = driver({6, 16, 21, 26, 34, 35, 44, 46, 46, 86}, 7, 5); 
      cout << "TEST-" << 6 << "..."
           <<  result
           << "\n";
  
      result = driver({86, 12, 80, 46, -12, 6}, 4, 0); 
      cout << "TEST-" << 7 << "..."
           <<  result
           << "\n";
  
      result = driver({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); 
      cout << "TEST-" << 8 << "..."
           <<  result
           << "\n";
  
      result = driver({71, 94, 91, 19, 85, 5, 87, 96, 66, 17, 95, 5, 32, 17, 93, 48, 46, 24}, 12, 7); 
      cout << "TEST-" << 9 << "..."
           <<  result
           << "\n";
  
      return 0;
  }
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;
  }