title: Maximum Product Subset Array
signature: long long maximumProductSubsetArray(vector<int> a, 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(long long actual, long long expected){
      return actual == expected;
  }
  
  string driver(vector<int> a, int n, long long expected){
      try
      {
          if (validateSolution(PLACEHOLDER_FN_NAME(a, 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({22, 62, 97}, 2, 1364); 
      cout << "TEST-" << 0 << "..."
           <<  result
           << "\n";
  
      result = driver({0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 1, 1, 1, 1, 1, 1}, 43, 1); 
      cout << "TEST-" << 1 << "..."
           <<  result
           << "\n";
  
      result = driver({-92, -78, -74, -72, -70, -66, -46, -44, -42, -34, -32, -30, -10, 18, 30, 34, 56, 64, 66}, 10, 510537135542353920); 
      cout << "TEST-" << 2 << "..."
           <<  result
           << "\n";
  
      result = driver({0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0}, 16, 1); 
      cout << "TEST-" << 3 << "..."
           <<  result
           << "\n";
  
      result = driver({1, 2, 3, 18, 20, 33, 38, 53, 55, 56, 67, 80, 83, 87, 90, 97, 98}, 9, 7895685600); 
      cout << "TEST-" << 4 << "..."
           <<  result
           << "\n";
  
      result = driver({0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 11, 1); 
      cout << "TEST-" << 5 << "..."
           <<  result
           << "\n";
  
      return 0;
  }
source_py: def maximum_product_subset_array(a, n):
      if (n == 1):
          return a[0]
      max_neg = (- 999999999999)
      count_neg = 0
      count_zero = 0
      prod = 1
      for i in range(n):
          if (a[i] == 0):
              count_zero += 1
              continue
          if (a[i] < 0):
              count_neg += 1
              max_neg = max(max_neg, a[i])
          prod = (prod * a[i])
      if (count_zero == n):
          return 0
      if (count_neg & 1):
          if ((count_neg == 1) and (count_zero > 0) and ((count_zero + count_neg) == n)):
              return 0
          prod = int((prod / max_neg))
      return prod
source_cpp: int maximum_product_subset_array(vector<int> a, int n) {
    if ( n == 1 ) return a [ 0 ];
    int max_neg = INT_MIN;
    int count_neg = 0, count_zero = 0;
    int prod = 1;
    for ( int i = 0;
    i < n;
    i ++ ) {
      if ( a [ i ] == 0 ) {
        count_zero ++;
        continue;
      }
      if ( a [ i ] < 0 ) {
        count_neg ++;
        max_neg = max ( max_neg, a [ i ] );
      }
      prod = prod * a [ i ];
    }
    if ( count_zero == n ) return 0;
    if ( count_neg & 1 ) {
      if ( count_neg == 1 && count_zero > 0 && count_zero + count_neg == n ) return 0;
      prod = prod / max_neg;
    }
    return prod;
  }