Given an array a, we have to find maximum product possible with the subset of elements present in the array. The maximum product can be single element also.
Examples:
Input : a[] = { -1, -1, -2, 4, 3 }
Output : 24
Explanation : Maximum product will be ( -2 * -1 * 4 * 3 ) = 24
Input : a[] = { -1, 0 }
Output : 0
Explanation : 0(single element) is maximum product possible
Input : a[] = { 0, 0, 0 }
Output : 0
A simple solution is to generate all subsets, find product of every subset and return maximum product.
A better solution is to use the below facts.
- If there are even number of negative numbers and no zeros, result is simply product of all
- If there are odd number of negative numbers and no zeros, result is product of all except the largest valued negative number.
- If there are zeros, result is product of all except these zeros with one exceptional case. The exceptional case is when there is one negative number and all other elements are 0. In this case, result is 0.
Below is the implementation of avove approach:
C++
// CPP program to find maximum product of // a subset. #include <bits/stdc++.h> using namespace std; int maxProductSubset(int a[], int n) { if (n == 1) return a[0]; // Find count of negative numbers, count // of zeros, maximum valued negative number // and product of non-zero numbers int max_neg = INT_MIN; int count_neg = 0, count_zero = 0; int prod = 1; for (int i = 0; i < n; i++) { // If number is 0, we don't // multiply it with product. if (a[i] == 0) { count_zero++; continue; } // Count negatives and keep // track of maximum valued negative. if (a[i] < 0) { count_neg++; max_neg = max(max_neg, a[i]); } prod = prod * a[i]; } // If there are all zeros if (count_zero == n) return 0; // If there are odd number of // negative numbers if (count_neg & 1) { // Exceptional case: There is only // negative and all other are zeros if (count_neg == 1 && count_zero > 0 && count_zero + count_neg == n) return 0; // Otherwise result is product of // all non-zeros divided by maximum // valued negative. prod = prod / max_neg; } return prod; } int main() { int a[] = { -1, -1, -2, 4, 3 }; int n = sizeof(a) / sizeof(a[0]); cout << maxProductSubset(a, n); return 0; } |
Java
// Java program to find maximum product of // a subset. class GFG { static int maxProductSubset(int a[], int n) { if (n == 1) { return a[0]; } // Find count of negative numbers, count // of zeros, maximum valued negative number // and product of non-zero numbers int max_neg = Integer.MIN_VALUE; int count_neg = 0, count_zero = 0; int prod = 1; for (int i = 0; i < n; i++) { // If number is 0, we don't // multiply it with product. if (a[i] == 0) { count_zero++; continue; } // Count negatives and keep // track of maximum valued negative. if (a[i] < 0) { count_neg++; max_neg = Math.max(max_neg, a[i]); } prod = prod * a[i]; } // If there are all zeros if (count_zero == n) { return 0; } // If there are odd number of // negative numbers if (count_neg % 2 == 1) { // Exceptional case: There is only // negative and all other are zeros if (count_neg == 1 && count_zero > 0 && count_zero + count_neg == n) { return 0; } // Otherwise result is product of // all non-zeros divided by maximum // valued negative. prod = prod / max_neg; } return prod; } // Driver code public static void main(String[] args) { int a[] = {-1, -1, -2, 4, 3}; int n = a.length; System.out.println(maxProductSubset(a, n)); } } /* This JAVA code is contributed by Rajput-Ji*/ |
Python3
# Python3 program to find maximum product # of a subset. def maxProductSubset(a, n): if n == 1: return a[0] # Find count of negative numbers, count # of zeros, maximum valued negative # number and product of non-zero numbers max_neg = -999999999999 count_neg = 0 count_zero = 0 prod = 1 for i in range(n): # If number is 0, we don't # multiply it with product. if a[i] == 0: count_zero += 1 continue # Count negatives and keep # track of maximum valued negative. if a[i] < 0: count_neg += 1 max_neg = max(max_neg, a[i]) prod = prod * a[i] # If there are all zeros if count_zero == n: return 0 # If there are odd number of # negative numbers if count_neg & 1: # Exceptional case: There is only # negative and all other are zeros if (count_neg == 1 and count_zero > 0 and count_zero + count_neg == n): return 0 # Otherwise result is product of # all non-zeros divided by maximum # valued negative. prod = int(prod / max_neg) return prod # Driver Code if __name__ == '__main__': a = [ -1, -1, -2, 4, 3 ] n = len(a) print(maxProductSubset(a, n)) # This code is contributed by PranchalK |
C#
// C# Java program to find maximum // product of a subset. using System; class GFG { static int maxProductSubset(int []a, int n) { if (n == 1) { return a[0]; } // Find count of negative numbers, // count of zeros, maximum valued // negative number and product of // non-zero numbers int max_neg = int.MinValue; int count_neg = 0, count_zero = 0; int prod = 1; for (int i = 0; i < n; i++) { // If number is 0, we don't // multiply it with product. if (a[i] == 0) { count_zero++; continue; } // Count negatives and keep // track of maximum valued negative. if (a[i] < 0) { count_neg++; max_neg = Math.Max(max_neg, a[i]); } prod = prod * a[i]; } // If there are all zeros if (count_zero == n) { return 0; } // If there are odd number of // negative numbers if (count_neg % 2 == 1) { // Exceptional case: There is only // negative and all other are zeros if (count_neg == 1 && count_zero > 0 && count_zero + count_neg == n) { return 0; } // Otherwise result is product of // all non-zeros divided by maximum // valued negative. prod = prod / max_neg; } return prod; } // Driver code public static void Main() { int []a = {-1, -1, -2, 4, 3}; int n = a.Length; Console.Write(maxProductSubset(a, n)); } } // This code is contributed by Rajput-Ji |
PHP
<?php // PHP program to find maximum // product of a subset. function maxProductSubset($a, $n) { if ($n == 1) return $a[0]; // Find count of negative numbers, // count of zeros, maximum valued // negative number and product of // non-zero numbers $max_neg = PHP_INT_MIN; $count_neg = 0; $count_zero = 0; $prod = 1; for ($i = 0; $i < $n; $i++) { // If number is 0, we don't // multiply it with product. if ($a[$i] == 0) { $count_zero++; continue; } // Count negatives and keep // track of maximum valued negative. if ($a[$i] < 0) { $count_neg++; $max_neg = max($max_neg, $a[$i]); } $prod = $prod * $a[$i]; } // If there are all zeros if ($count_zero == $n) return 0; // If there are odd number of // negative numbers if ($count_neg & 1) { // Exceptional case: There is only // negative and all other are zeros if ($count_neg == 1 && $count_zero > 0 && $count_zero + $count_neg == $n) return 0; // Otherwise result is product of // all non-zeros divided by maximum // valued negative. $prod = $prod / $max_neg; } return $prod; } // Driver Code $a = array(-1, -1, -2, 4, 3 ); $n = sizeof($a); echo maxProductSubset($a, $n); // This code is contributed // by Akanksha Rai ?> |
24
Time Complexity : O(n)
Auxiliary Space : O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Largest possible Subset from an Array such that no element is K times any other element in the Subset
- Minimum product subset of an array
- Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k
- Prime Subset Product Problem
- Maximize count of subsets having product of smallest element and size of the subset at least X
- Maximum Product Subarray | Added negative product case
- Find the sum of maximum difference possible from all subset of a given array.
- Maximum number of elements greater than X after equally distributing subset of array
- Maximum Subset Sum possible by negating the entire sum after selecting the first Array element
- Maximum element in an array such that its previous and next element product is maximum
- Find whether an array is subset of another array | Added Method 3
- Array with GCD of any of its subset belongs to the given array
- Find whether an array is subset of another array using Map
- Find the maximum subset XOR of a given set
- Largest subset with maximum difference as 1
- Size of the smallest subset with maximum Bitwise OR
- Maximum subset sum such that no two elements in set have same digit in them
- Maximum sum subset having equal number of positive and negative elements
- Find subset with maximum sum under given condition
- Maximum size subset with given sum using Backtracking
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

