Maximum product quadruple (sub-sequence of size 4) in array in C++


In this problem, we are given an array arr[]. Our task is to create a program to find the Maximum product quadruple (sub-sequence of size 4) in array in C++.

Code description − Here, we need to find a quadruple (sub-sequence of size 4) such that the product of all elements is maximum.

Let’s take an example to understand the problem,

Input

arr[] = {4, -2, 5, -6, 8}

Output

840

Explanation

The quadruple, (-3, 5, -7, 8) with product 840.

Solution Approach

There can be multiple solutions to a given problem.

One simple solution is using the direct method by traversing the array. Then finding all possible quadruples in the array. Finding their product and then comparing it to find the maximum product quadruple.

Program to illustrate the working of our solution,

Example

 Live Demo

#include <iostream>
using namespace std;
int max(int a, int b){
   if(a > b)
      return a;
      return b;
}
int findMaxProdQuad(int arr[], int n){
   int maxProd = 0;
   int prod = 1;
   for (int i = 0; i <= n - 4; i++)
   for (int j = i + 1; j <= n - 3; j++)
   for (int k = j + 1; k <= n - 2; k++)
   for (int l = k + 1; l <= n - 1; l++) {
      prod = arr[i] * arr[j] * arr[k] * arr[l];
      maxProd = max(maxProd, prod);
      prod = 1;
   }
   return maxProd;
}
int main(){
   int arr[] = {4, -2, 5, -6, 8};
   int n = sizeof(arr) / sizeof(arr[0]);
   cout<<"Maximum product of quadruple is "<<findMaxProdQuad(arr, n);
   return 0;
}

Output

Maximum product of quadruple is 480

Another way to find the quadruple with maximum product is to find four maximum elements of the array and four minimum elements of the array.

Let’s say mx1, mx2, mx3, mx4 are the first four maximum numbers. And mn1, mn2, mn3, mn4 are the first four minimum numbers of the array. Then find the values of

1. mx1 * mx2 * mx3 * mx4
2. mn1 * mn2 * mn3 * mn4
3. mx1 * mx2 * mn1 * mn2

And return the maximum of these three product values which will give the maximum product quadruple. And all cases are considered.

Program to show the implementation of our algorithm

Example

 Live Demo

#include <iostream>
using namespace std;
int max(int a, int b){
   if(a > b)
      return a;
      return b;
}
int findMaxProdQuad(int arr[], int n) {
   int mx1 = -1000, mx2 = -1000, mx3 = -10000, mx4 = -1000;
   int mn1 = 1000, mn2 = 1000, mn3 = 1000, mn4 = 1000;
   for (int i = 0; i < n; i++) {
      if(arr[i] < mn1){
         mn4 = mn3;
         mn3 = mn2;
         mn2 = mn1;
         mn1 = arr[i];
      }
      else if(arr[i] < mn2){
         mn4 = mn3;
         mn3 = mn2;
         mn2 = arr[i];
      }
      else if(arr[i] < mn3){
         mn4 = mn3;
         mn3 = arr[i];
      }
      else if(arr[i] < mn4){
         mn4 = arr[i];
      }
      if(arr[i] > mx1){
         mx4 = mx3;
         mx3 = mx2;
         mx2 = mx1;
         mx1 = arr[i];
      }
      else if(arr[i] > mx2){
         mx4 = mx3;
         mx3 = mx2;
         mx2 = arr[i];
      }
      else if(arr[i] > mx3){
         mx4 = mx3;
         mx3 = arr[i];
      }
      else if(arr[i] > mx4){
         mx4 = arr[i];
      }
   }
   int maxVal = max ((mx1 * mx2 * mx3 * mx4), (mn1 * mn2 * mn3 * mn4));
   maxVal = max(maxVal, (mx1 * mx2 * mn1 * mn2));
   return maxVal;
}
int main() {
   int arr[] = {4, -2, 5, -6, 8};
   int n = sizeof(arr) / sizeof(arr[0]);
   cout<<"Maximum product of quadruple is "<<findMaxProdQuad(arr, n);
   return 0;
}

Output

Maximum product of quadruple is 480

One more approach could be sorting the array. Then the four maximums and four minimums will be the end and start of the array respectively. Then solve as in the above solution by finding the max of three combinations of maximum and minimums.

Program to show the implementation of our approach

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int findMaxProdQuad(int arr[], int n){
   sort(arr, arr + n);
   int maxVal = max((arr[n-1] * arr[n-2] * arr[n-3] * arr[n-4]), (arr[0] *
   arr[1] * arr[2] * arr[3]));
   maxVal = max(maxVal, (arr[n-1] * arr[n-2] * arr[0] * arr[1]));
   return maxVal;
}
int main(){
   int arr[] = {4, -2, 5, -6, 8};
   int n = sizeof(arr) / sizeof(arr[0]);
   cout<<"Maximum product of quadruple is "<<findMaxProdQuad(arr, n);
   return 0;
}

Output

Maximum product of quadruple is 480

Updated on: 15-Sep-2020

287 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements