
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Maximum and Minimum Product Subsets in C++
We are given with an array of integers of size N. The goal here is to find the Maximum and Minimum Product Subsets. We will do this by taking two product variables, one for minimum product found so far minProd and other for maximum product found so far, maxProd.
While traversing the array we will multiply each element with both minProd and maxProd. Also keep a check on previous maximum product, previous minimum product, current maximum product, current minimum product and the current element itself.
Input
Arr[]= { 1,2,5,0,2 }
Output
Maximum Product: 20 Minimum Product: 0
Explanation − Starting from the second element in the array with maxProd and minProd values initialized with 1 ( first element ).
Arr[1]: 1*2=2, 1*2=2, maxProd=2, minProd=1 Arr[2]: 2*5=10, 1*5=5, maxProd=10, minProd=1 Arr[3]: 10*0=0, 1*0=0, maxProd=10, minProd=0 Arr[4]: 10*2=20, 0*2=0, maxProd=20, minProd=0
Input
Arr[]= { -1,2,-5,0,2 }
Output
Maximum Product: 20 Minimum Product: -20
Explanation − For maximum product, subset has elements- { -1,2,-5,2 }
For minimum product, subset has elements- { 2,-5,2 }
Approach used in the below program is as follows
The integer array Arr[] contains the positive and negative integers.
Variable size contains the length of the array.
The function getProductSubset(int arr[], int n) takes the array as input and returns the maximum and minimum product of elements obtained.
Variables curMin, curMax are used to store the current maximum and minimum product found. Initially arr[0].
Variables prevMin, prevMax are used to store the previous maximum and minimum product found. Initially arr[0].
Variables maxProd and minProd are used to store the final maximum and minimum products obtained.
Start traversing the array from 2nd element, arr[1] till last index.
For maximum product, multiply current arr[i] with prevMax and prevMin. Store maximum product in curMax. If this curMax>prevMax, update curMax with prevMax.
Update maxProd with curMax if curMax>maxProd.
At last update prevMax with curMax for next iteration.
Do the same steps as above for prevMin, curMin and minProd by changing comparisons.
Print the results obtained in maxProd and minProd after the loop ends.
Example
#include <iostream> using namespace std; void getProductSubset(int arr[], int n){ // Initialize all products with arr[0] int curMax = arr[0]; int curMin = arr[0]; int prevMax = arr[0]; int prevMin= arr[0]; int maxProd = arr[0]; int minProd = arr[0]; int temp1=0,temp2=0,temp3=0; // Process all elements after arr[0] for (int i = 1; i < n; ++i){ /* Current maximum product is maximum of following 1) prevMax * current arr[i] (when arr[i] is +ve) 2) prevMin * current arr[i] (when arr[i] is -ve) 3) current arr[i] 4) Previous max product */ temp1=prevMax*arr[i]; temp2=prevMin*arr[i]; temp3=temp1>temp2?temp1:temp2; curMax = temp3>arr[i]?temp3:arr[i]; curMax = curMax>prevMax?curMax:prevMax; /* Current minimum product is minimum of following 1) prevMin * current arr[i] (when arr[i] is +ve) 2) prevMax * current arr[i] (when arr[i] is -ve) 3) current arr[i] 4) Previous min product */ temp1=prevMax*arr[i]; temp2=prevMin*arr[i]; temp3=temp1<temp2?temp1:temp2; curMin = temp3<arr[i]?temp3:arr[i]; curMin = curMin<prevMin?curMin:prevMin; maxProd = maxProd>curMax?maxProd:curMax; minProd = minProd<curMin?minProd:curMin; // copy current values to previous values prevMax = curMax; prevMin = curMin; } std::cout<<"Maximum Subset Product: "<<maxProd; std::cout<<"\nMinimum Subset Product: "<<minProd; } int main(){ int Arr[] = {-4, -3, 1, 2, 0, 8, 1}; // int arr[] = {-4, 1,1, 3, 5,7}; int size = 7; getProductSubset(Arr,size ) ; return 0; }
Output
Maximum Subset Product: 192 Minimum Subset Product: -64
- Related Articles
- Product of maximum in first array and minimum in second in C
- Maximum number of trailing zeros in the product of the subsets of size k in C++
- Product of all Subsequences of size K except the minimum and maximum Elements in C++
- How to read Maximum and Minimum temperature in Six's Maximum and Minimum Thermometer?
- What Is Maximum and Minimum ?
- Minimum and Maximum Priority Threads in Java
- Get maximum and minimum value in MongoDB?
- Getting Minimum and Maximum Value in MySQL
- Maximum difference between two subsets of m elements in C
- Maximum possible difference of two subsets of an array in C++
- Maximum Product Subarray in Python
- Maximum and Minimum in a square matrix in C++
- Maximum and minimum of an array using minimum number of comparisons in C
- Maximum Product Subarray | Added negative product case in C++
- Maximum and Minimum K elements in Tuple using Python
