Maximum and Minimum Product Subsets in C++

C++Server Side ProgrammingProgramming

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

 Live Demo

#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
raja
Published on 28-Jul-2020 14:23:45
Advertisements