title: Count Subarrays With Same Even And Odd Elements
signature: function count_subarrays_with_same_even_and_odd_elements(arr, n)
test_code: 
  
  -- SOLUTION CODE
  -- ============================================
  PLACEHOLDER_CODE_BODY
  
  -- TESTING CODE 
  -- ============================================
  function isCorrect(actual, expected)
      if type(expected) ~= type(actual) then
          return false
      end
      
      if type(expected) == 'table' then
          for k,v_expected in pairs(expected) do
              
              if actual[k] == nil then
                  return false
              end
              local v_actual = actual[k]
              if not isCorrect(v_actual, v_expected) then
                  return false
              end
              actual[k]=nil
          end
      
          if next(actual) then 
              return false
          end
          return true
      else
          return actual == expected
      end
  end
  
  function validateSolution(actual, expected)
      return isCorrect(actual,expected)
  end
  
  function driver(arr, n, expected)
      local exec_result;
      local status, error_msg = pcall(function () 
          exec_result=PLACEHOLDER_FN_NAME(arr, n) 
      end)
      if status then
          if validateSolution(exec_result,expected) then
              return "PASSED"
          end
          return "FAILED"
      else
          return string.format("ERROR=%s",string.gsub(error_msg,".*:[%d]+: ",""))
      end
  end
      
  function main()
      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)
      print(string.format("TEST-0...%s",result))
  
      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)
      print(string.format("TEST-1...%s",result))
  
      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)
      print(string.format("TEST-2...%s",result))
  
      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)
      print(string.format("TEST-3...%s",result))
  
      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)
      print(string.format("TEST-4...%s",result))
  
      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)
      print(string.format("TEST-5...%s",result))
  
      result = driver({6, 16, 21, 26, 34, 35, 44, 46, 46, 86}, 7, 5)
      print(string.format("TEST-6...%s",result))
  
      result = driver({86, 12, 80, 46, -12, 6}, 4, 0)
      print(string.format("TEST-7...%s",result))
  
      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)
      print(string.format("TEST-8...%s",result))
  
      result = driver({71, 94, 91, 19, 85, 5, 87, 96, 66, 17, 95, 5, 32, 17, 93, 48, 46, 24}, 12, 7)
      print(string.format("TEST-9...%s",result))
  
  end
  
  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;
  }