title: Count Subarrays With Same Even And Odd Elements
signature: count_subarrays_with_same_even_and_odd_elements <- function(arr, n) {
test_code: 
  
  
  # SOLUTION CODE
  # ============================================
  PLACEHOLDER_CODE_BODY
  
  # TESTING CODE 
  # ============================================
  validate_solution <- function(actual, expected) {
      toString(actual)==toString(expected)
  }
  
  driver <- function(arr, n, expected){
      if (validate_solution(PLACEHOLDER_FN_NAME(arr, n), expected)){
          "PASSED"
      } else{
          "FAILED"
      }
  }
  
  main <- function() {
      cat(sprintf("TEST-0...%s\n",driver(
          list(7L, 8L, 12L, 13L, 14L, 19L, 20L, 22L, 28L, 30L, 31L, 31L, 32L, 34L, 34L, 39L, 39L, 43L, 45L, 46L, 47L, 62L, 63L, 63L, 65L, 66L, 69L, 69L, 71L, 76L, 79L, 82L, 83L, 88L, 89L, 92L, 93L, 95L, 97L, 97L), 26L, 
          50L
      )))
  
      cat(sprintf("TEST-1...%s\n",driver(
          list(20L, -98L, -44L, -82L, 28L, 20L, -76L, -16L, 42L, 0L, -88L, 74L, 56L, 6L, -68L, -30L, 28L, 88L, 58L, -78L, 46L, 2L), 15L, 
          0L
      )))
  
      cat(sprintf("TEST-2...%s\n",driver(
          list(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 24L, 
          11L
      )))
  
      cat(sprintf("TEST-3...%s\n",driver(
          list(44L, 4L, 29L, 83L, 28L, 75L, 58L, 89L, 40L, 38L, 29L, 45L, 21L, 87L, 97L, 42L, 95L, 20L, 48L, 38L, 15L, 67L, 23L, 81L, 50L, 53L, 64L, 67L, 30L, 13L, 52L, 56L, 87L, 10L, 80L, 38L, 31L, 19L), 23L, 
          43L
      )))
  
      cat(sprintf("TEST-4...%s\n",driver(
          list(-94L, -94L, -94L, -90L, -88L, -86L, -86L, -82L, -78L, -76L, -74L, -68L, -64L, -62L, -62L, -60L, -52L, -48L, -48L, -46L, -44L, -44L, -32L, -28L, -22L, -12L, -12L, -8L, -8L, -4L, 4L, 6L, 10L, 14L, 28L, 40L, 42L, 52L, 56L, 60L, 60L, 60L, 64L, 68L, 70L, 82L, 82L, 84L, 96L), 48L, 
          0L
      )))
  
      cat(sprintf("TEST-5...%s\n",driver(
          list(1L, 0L, 0L, 0L, 1L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 1L), 27L, 
          68L
      )))
  
      cat(sprintf("TEST-6...%s\n",driver(
          list(6L, 16L, 21L, 26L, 34L, 35L, 44L, 46L, 46L, 86L), 7L, 
          5L
      )))
  
      cat(sprintf("TEST-7...%s\n",driver(
          list(86L, 12L, 80L, 46L, -12L, 6L), 4L, 
          0L
      )))
  
      cat(sprintf("TEST-8...%s\n",driver(
          list(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 15L, 
          4L
      )))
  
      cat(sprintf("TEST-9...%s\n",driver(
          list(71L, 94L, 91L, 19L, 85L, 5L, 87L, 96L, 66L, 17L, 95L, 5L, 32L, 17L, 93L, 48L, 46L, 24L), 12L, 
          7L
      )))
  
  }
  
  main()
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;
  }