title: Maximum Product Subset Array
signature: function maximum_product_subset_array(a, n)
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;
  }
question_info.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(a, n, expected)
      local exec_result;
      local status, error_msg = pcall(function () 
          exec_result=PLACEHOLDER_FN_NAME(a, 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({22, 62, 97}, 2, 1364)
      print(string.format("TEST-0...%s",result))
  
      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)
      print(string.format("TEST-1...%s",result))
  
      result = driver({-92, -78, -74, -72, -70, -66, -46, -44, -42, -34, -32, -30, -10, 18, 30, 34, 56, 64, 66}, 10, 510537135542353920)
      print(string.format("TEST-2...%s",result))
  
      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)
      print(string.format("TEST-3...%s",result))
  
      result = driver({1, 2, 3, 18, 20, 33, 38, 53, 55, 56, 67, 80, 83, 87, 90, 97, 98}, 9, 7895685600)
      print(string.format("TEST-4...%s",result))
  
      result = driver({0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 11, 1)
      print(string.format("TEST-5...%s",result))
  
  end
  
  main()
question_info.entry_cls_name: Solution
question_info.test_list: [{"idx": 0, "outputs": 1364, "inputs": {"a": [22, 62, 97], "n": 2}}, {"idx": 1, "outputs": 1, "inputs": {"a": [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], "n": 43}}, {"idx": 2, "outputs": 510537135542353920, "inputs": {"a": [-92, -78, -74, -72, -70, -66, -46, -44, -42, -34, -32, -30, -10, 18, 30, 34, 56, 64, 66], "n": 10}}, {"idx": 3, "outputs": 1, "inputs": {"a": [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], "n": 16}}, {"idx": 4, "outputs": 7895685600, "inputs": {"a": [1, 2, 3, 18, 20, 33, 38, 53, 55, 56, 67, 80, 83, 87, 90, 97, 98], "n": 9}}, {"idx": 5, "outputs": 1, "inputs": {"a": [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], "n": 11}}]
question_info.extension: lua
question_info.entry_fn_name: maximum_product_subset_array
question_info.test_case_ids: 0 1 2 3 4 5
question_info.commands: lua __FILENAME__