title: Minimum Product Subset Array
signature: func minimumProductSubsetArray(a []int, n int) int64 {
test_code: package main
  
  import (
      "fmt"
      "reflect"
      "math"
      "sort"
  )
  
  // SOLUTION CODE
  // ===================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE
  // ===================================
  func validateSolution(actual int64, expected int64) bool {
      return reflect.DeepEqual(actual,expected)
  }
  
  func driver(a []int, n int, expected int64) string {
      if validateSolution(PLACEHOLDER_FN_NAME(a, n),expected) {
              return "PASSED"
      }
      return "FAILED"
  
  }
  
  func main() {
  
      // Use imports to avoid undefined errors
      var _ = reflect.DeepEqual
      _ = math.Abs
      _ = sort.Sort
      result := ""
      result = driver([]int{3, 6, 7, 8, 8, 9, 12, 12, 12, 13, 15, 15, 15, 16, 18, 18, 18, 19, 20, 21, 22, 22, 23, 28, 29, 30, 30, 33, 33, 35, 35, 36, 40, 43, 58, 63, 73, 78, 82, 83, 84, 87, 89, 89, 92, 94}, 
  	23, 3)
      fmt.Printf("TEST-0...%s\n", result)
      
      result = driver([]int{18, -6, -8, 98, 66, -86, 24, 6, 58, 74, 82}, 
  	10, -297032800813056)
      fmt.Printf("TEST-1...%s\n", result)
      
      result = driver([]int{0, 0, 0, 0, 0, 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}, 
  	25, 0)
      fmt.Printf("TEST-2...%s\n", result)
      
      result = driver([]int{97, 79, 93, 41, 76, 34, 94, 57, 63, 98, 52, 62, 96, 7, 63, 44, 55, 43, 36, 66, 35, 14, 24, 40, 26, 16, 67, 19, 31, 86, 64, 93, 85, 86, 66, 24, 73, 86, 45, 99, 25, 98, 38, 57}, 
  	30, 7)
      fmt.Printf("TEST-3...%s\n", result)
      
      result = driver([]int{-58, -48, -46, -36, 0, 18}, 
  	3, -128064)
      fmt.Printf("TEST-4...%s\n", result)
      
      result = driver([]int{1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0}, 
  	36, 0)
      fmt.Printf("TEST-5...%s\n", result)
      
      result = driver([]int{1, 3, 5, 15, 18, 19, 21, 23, 29, 29, 33, 33, 34, 37, 39, 43, 43, 68, 73, 74, 75, 84, 87, 88, 89, 90, 93}, 
  	18, 1)
      fmt.Printf("TEST-6...%s\n", result)
      
      result = driver([]int{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 
  	20, 0)
      fmt.Printf("TEST-7...%s\n", result)
      
      result = driver([]int{74, 74, 8, 74, 85, 41, 31, 3, 84, 46, 73, 39, 64, 72, 28, 83, 98, 27, 64, 7, 95, 37, 10, 38, 77, 32, 69, 72, 62, 96, 5, 81, 34, 96, 80, 25, 38}, 
  	33, 3)
      fmt.Printf("TEST-8...%s\n", result)
      
          
  }
source_py: def minimum_product_subset_array(a, n):
      if (n == 1):
          return a[0]
      max_neg = float('-inf')
      min_pos = float('inf')
      count_neg = 0
      count_zero = 0
      prod = 1
      for i in range(0, n):
          if (a[i] == 0):
              count_zero = (count_zero + 1)
              continue
          if (a[i] < 0):
              count_neg = (count_neg + 1)
              max_neg = max(max_neg, a[i])
          if (a[i] > 0):
              min_pos = min(min_pos, a[i])
          prod = (prod * a[i])
      if ((count_zero == n) or ((count_neg == 0) and (count_zero > 0))):
          return 0
      if (count_neg == 0):
          return min_pos
      if (((count_neg & 1) == 0) and (count_neg != 0)):
          prod = int((prod / max_neg))
      return prod
source_cpp: int minimum_product_subset_array(vector<int> a, int n) {
    if ( n == 1 ) return a [ 0 ];
    int max_neg = INT_MIN;
    int min_pos = INT_MAX;
    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 ] );
      }
      if ( a [ i ] > 0 ) min_pos = min ( min_pos, a [ i ] );
      prod = prod * a [ i ];
    }
    if ( count_zero == n || ( count_neg == 0 && count_zero > 0 ) ) return 0;
    if ( count_neg == 0 ) return min_pos;
    if ( ! ( count_neg & 1 ) && count_neg != 0 ) {
      prod = prod / max_neg;
    }
    return prod;
  }