Maximum and Minimum Product Subsets
Given a set, we need to find maximum and minimum possible product among all subsets of the set.
Examples:
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.
Input : arr[] = {4, -2, 5};
Output: Maximum product = 20
Minimum product = -40
Maximum product is obtained by multiplying
4 5
Minimum product is obtained by multiplying
4, -2, 5
Input : arr[] = {-4, -2, 3, 7, 5, 0, 1};
Output: Maximum product = 840
Minimum product = -420
Maximum product is obtained by multiplying
-4, -2, 3, 7, 5
Minimum product is obtained by multiplying
-4, 3, 7, 5 As array can have negative value, zero and positive value, this problem can have lot of edge cases, if not attacked properly. Below given solution maintains maximum product and minimum product at current index and previous index and at any instant current product takes value from previous max or previous min multiplied with current element, depending on the sign of current element. For example, if we are finding maximum product then current maximum will be previous max times current value if current element is positive otherwise previous min times current value if current element is negative. Same procedure is applied for finding minimum product also.
Please see below simple code to understand.
C++
// C++ program to find maximum and minimum// product from an array#include <bits/stdc++.h>using namespace std;// method returns maximum and minimum obtainable// product of array arrpair<int, int> getMaxandMinProduct(int arr[], int n){ // Initialize all products with arr[0] int curMaxProduct = arr[0]; int curMinProduct = arr[0]; int prevMaxProduct = arr[0]; int prevMinProduct = arr[0]; int maxProduct = arr[0]; int minProduct = arr[0]; // Process all elements after arr[0] for (int i = 1; i < n; ++i) { /* Current maximum product is maximum of following 1) prevMax * curelement (when curelement is +ve) 2) prevMin * curelement (when curelement is -ve) 3) Element itself 4) Previous max product */ curMaxProduct = max(prevMaxProduct * arr[i], max(prevMinProduct * arr[i], arr[i])); curMaxProduct = max(curMaxProduct, prevMaxProduct); /* Current min product computation is Similar to that of current max profuct */ curMinProduct = min(prevMaxProduct * arr[i], min(prevMinProduct * arr[i], arr[i])); curMinProduct = min(curMinProduct, prevMinProduct); maxProduct = max(maxProduct, curMaxProduct); minProduct = min(minProduct, curMinProduct); // copy current values to previous values prevMaxProduct = curMaxProduct; prevMinProduct = curMinProduct; } return make_pair(minProduct, maxProduct);}// driver code to test above methodsint main(){ int arr[] = {-4, -2, 3, 7, 5, 0, 1}; int n = sizeof(arr) / sizeof(int); pair<int, int> product = getMaxandMinProduct(arr, n); printf("Minimum product is %d and " "Maximum product is %dn", product.first, product.second); return 0;} |
Java
// Java program to find maximum and minimum// product from an arrayclass GFG{static class pair{ int first, second; public pair(int first, int second) { this.first = first; this.second = second; }}// method returns maximum and minimum obtainable// product of array arrstatic pair getMaxandMinProduct(int arr[], int n){ // Initialize all products with arr[0] int curMaxProduct = arr[0]; int curMinProduct = arr[0]; int prevMaxProduct = arr[0]; int prevMinProduct = arr[0]; int maxProduct = arr[0]; int minProduct = arr[0]; // Process all elements after arr[0] for (int i = 1; i < n; ++i) { /* Current maximum product is maximum of following 1) prevMax * curelement (when curelement is +ve) 2) prevMin * curelement (when curelement is -ve) 3) Element itself 4) Previous max product */ curMaxProduct = Math.max(prevMaxProduct * arr[i], Math.max(prevMinProduct * arr[i], arr[i])); curMaxProduct = Math.max(curMaxProduct, prevMaxProduct); /* Current min product computation is Similar to that of current max profuct */ curMinProduct = Math.min(prevMaxProduct * arr[i], Math.min(prevMinProduct * arr[i], arr[i])); curMinProduct = Math.min(curMinProduct, prevMinProduct); maxProduct = Math.max(maxProduct, curMaxProduct); minProduct = Math.min(minProduct, curMinProduct); // copy current values to previous values prevMaxProduct = curMaxProduct; prevMinProduct = curMinProduct; } return new pair(minProduct, maxProduct);}// Driver Codepublic static void main(String[] args){ int arr[] = {-4, -2, 3, 7, 5, 0, 1}; int n = arr.length; pair product = getMaxandMinProduct(arr, n); System.out.printf("Minimum product is %d and " + "Maximum product is %d", product.first, product.second); }}// This code is contributed by Rajput-Ji |
Python3
# Python3 program to find maximum and# minimum product from an array# method returns maximum and minimum# obtainable product of array arrdef getMaxandMinProduct(arr, n): # Initialize all products with arr[0] curMaxProduct = arr[0] curMinProduct = arr[0] prevMaxProduct = arr[0] prevMinProduct = arr[0] maxProduct = arr[0] minProduct = arr[0] # Process all elements after arr[0] for i in range(1, n): # Current maximum product is maximum of following # 1) prevMax * curelement (when curelement is +ve) # 2) prevMin * curelement (when curelement is -ve) # 3) Element itself # 4) Previous max product curMaxProduct = max(prevMaxProduct * arr[i], max(prevMinProduct * arr[i], arr[i])) curMaxProduct = max(curMaxProduct, prevMaxProduct) # Current min product computation is Similar to # that of current max profuct curMinProduct = min(prevMaxProduct * arr[i], min(prevMinProduct * arr[i], arr[i])) curMinProduct = min(curMinProduct, prevMinProduct) maxProduct = max(maxProduct, curMaxProduct) minProduct = min(minProduct, curMinProduct) # copy current values to previous values prevMaxProduct = curMaxProduct prevMinProduct = curMinProduct return (minProduct, maxProduct)# Driver Codeif __name__ == "__main__": arr = [-4, -2, 3, 7, 5, 0, 1] n = len(arr) product = getMaxandMinProduct(arr, n) print("Minimum product is", product[0], "and", "Maximum product is", product[1])# This code is contributed by# sanjeev2552 |
C#
// C# program to find maximum and minimum// product from an arrayusing System;class GFG{public class pair{ public int first, second; public pair(int first, int second) { this.first = first; this.second = second; }}// method returns maximum and minimum// obtainable product of array arrstatic pair getMaxandMinProduct(int []arr, int n){ // Initialize all products with arr[0] int curMaxProduct = arr[0]; int curMinProduct = arr[0]; int prevMaxProduct = arr[0]; int prevMinProduct = arr[0]; int maxProduct = arr[0]; int minProduct = arr[0]; // Process all elements after arr[0] for (int i = 1; i < n; ++i) { /* Current maximum product is maximum of following 1) prevMax * curelement (when curelement is +ve) 2) prevMin * curelement (when curelement is -ve) 3) Element itself 4) Previous max product */ curMaxProduct = Math.Max(prevMaxProduct * arr[i], Math.Max(prevMinProduct * arr[i], arr[i])); curMaxProduct = Math.Max(curMaxProduct, prevMaxProduct); /* Current min product computation is Similar to that of current max profuct */ curMinProduct = Math.Min(prevMaxProduct * arr[i], Math.Min(prevMinProduct * arr[i], arr[i])); curMinProduct = Math.Min(curMinProduct, prevMinProduct); maxProduct = Math.Max(maxProduct, curMaxProduct); minProduct = Math.Min(minProduct, curMinProduct); // copy current values to previous values prevMaxProduct = curMaxProduct; prevMinProduct = curMinProduct; } return new pair(minProduct, maxProduct);}// Driver Codepublic static void Main(String[] args){ int []arr = {-4, -2, 3, 7, 5, 0, 1}; int n = arr.Length; pair product = getMaxandMinProduct(arr, n); Console.Write("Minimum product is {0} and " + "Maximum product is {1}", product.first, product.second);}}// This code is contributed by Princi Singh |
Javascript
<script>// Javascript program to find maximum and minimum// product from an array// method returns maximum and minimum obtainable// product of array arrfunction getMaxandMinProduct(arr, n){ // Initialize all products with arr[0] let curMaxProduct = arr[0]; let curMinProduct = arr[0]; let prevMaxProduct = arr[0]; let prevMinProduct = arr[0]; let maxProduct = arr[0]; let minProduct = arr[0]; // Process all elements after arr[0] for(let i = 1; i < n; ++i) { /* Current maximum product is maximum of following 1) prevMax * curelement (when curelement is +ve) 2) prevMin * curelement (when curelement is -ve) 3) Element itself 4) Previous max product */ curMaxProduct = Math.max(prevMaxProduct * arr[i], Math.max(prevMinProduct * arr[i], arr[i])); curMaxProduct = Math.max(curMaxProduct, prevMaxProduct); /* Current min product computation is Similar to that of current max profuct */ curMinProduct = Math.min(prevMaxProduct * arr[i], Math.min(prevMinProduct * arr[i], arr[i])); curMinProduct = Math.min(curMinProduct, prevMinProduct); maxProduct = Math.max(maxProduct, curMaxProduct); minProduct = Math.min(minProduct, curMinProduct); // Copy current values to previous values prevMaxProduct = curMaxProduct; prevMinProduct = curMinProduct; } return [minProduct, maxProduct];}// Driver codelet arr = [ -4, -2, 3, 7, 5, 0, 1 ];let n = arr.length;let product = getMaxandMinProduct(arr, n);document.write("Minimum product is " + product[0] + " and Maximum product is " + product[1]); // This code is contributed by _saurabh_jaiswal</script> |
Output:
Minimum product is -420 and Maximum product is 840
This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



