# Maximum product of a triplet (subsequence of size 3) in array in C++ Program.

In this problem, we are given an array arr[] consisting of n integers. Our task is to find the maximum product of a triplet (subsequence of size 3) in array. Here, we will be finding the triple with maximum product value and then return the product.

Let’s take an example to understand the problem,

## Input

arr[] = {9, 5, 2, 11, 7, 4}

## Output

693

## Explanation

Here, we will find the triplet that gives the maximum product of all elements of the array. maxProd = 9 * 11 * 7 = 693

## Solution Approach

There can be multiple solutions to the problem. We will be discussing them here,

## Method 1

Direct method In this method, we will directly loop through the array and then find all possible triplet. Find the product of elements of each triplet and return the maximum of all of them.

## Algorithm

Initialise

maxProd = −1000

Step 1:

Create three nested loops:
Loop 1:i −> 0 to n−3
Loop 2: j −> i to n−2
Loop 3: k −> j to n−1

Step 1.1

Find the product, prod = arr[i]*arr[j]*arr[k].

Step 1.2

if prod > maxProd −> maxProd = prod.

Step 3

return maxProd.

## Example

Program to show the implementation of our solution,

Live Demo

#include <iostream>
using namespace std;
int calcMaxProd(int arr[], int n){
int maxProd = −1000;
int prod;

for (int i = 0; i < n − 2; i++)
for (int j = i + 1; j < n − 1; j++)
for (int k = j + 1; k < n; k++){
prod = arr[i] * arr[j] * arr[k];
if(maxProd < prod)
maxProd = prod;
}
return maxProd;
}
int main(){
int arr[] = { 9, 5, 2, 11, 7, 4 };
int n = sizeof(arr) / sizeof(arr);
cout<<"Maximum product of a triplet in array is "<<calcMaxProd(arr, n);
return 0;
}

## Output

Maximum product of a triplet in array is 693

## Method 2

Using Sorting

In this method, we will sort the array in descending order. In sorted array, the maximum product triplet will be at,

(arr, arr, arr)
(arr, arr, arr)

We will return the maximum of the product of these triplets.

## Algorithm

Step 1

Sort the given array in descending order.

Step 2

Find product of triples,
maxTriplet1 = arr*arr*arr
maxTriplet2 = arr*arr[n−1]*arr[n−2]

Step 3

if( maxTriplet1 > maxTriplet2 ) −> return maxTriplet1

Step 4

else −> return maxTriplet2.

## Example

Program to illustrate the working of our solution,

Live Demo

#include <bits/stdc++.h>
using namespace std;
int calcMaxProd(int arr[], int n){
sort(arr, arr + n, greater<>());
int maxTriplet1 = arr*arr*arr;
int maxTriplet2 = arr*arr[n−1]*arr[n−2];
if(maxTriplet1 > maxTriplet2)
return maxTriplet1;
return maxTriplet2;
}
int main(){
int arr[] = { 9, 5, 2, 11, 7, 4 };
int n = sizeof(arr) / sizeof(arr);
cout<<"Maximum product of a triplet in array is
"<<calcMaxProd(arr, n);
return 0;
}

## Output

Maximum product of a triplet in array is 693

## Method 3

Finding triplet values.

As we now know that the maximum product triplet can be from either of the two triplets,

(maximum, second_max, third_max)
(maximum, minimum, second_min)

So, we can directly find these values by traversing the array, and then using the values, we will find the maximum product triplet.

## Algorithm

Initialise

max = -1000, secMax = -1000, thirdMax = -1000 , min = 10000, secMin = 10000

Step 1

loop the array i −> 0 to n−1.

Step 1.1

if(arr[i] > max) −> thirdMax = secMax, secMax = max, max
= arr[i]

Step 1.2

elseif(arr[i] > secMax) −> thirdMax = secMax, secMax =
arr[i]

Step 1.3

elseif(arr[i] > thirdMax) −> thirdMax = arr[i]

Step 1.4

if(arr[i] < min) −> secMin = min, min = arr[i]

Step 1.4

elseif(arr[i] < secMin) −> secMin = arr[i]

Step 2

triplet1 = max * secMax * thridMax
triplet2 = max * min * secMin

Step 3

if(triplet1 > triplet2) −> return triplet1

Step 4

else −> return triplet2

## Example

Program to illustrate the working of our solution,

Live Demo

#include <iostream>
using namespace std;
int calcMaxProd(int arr[], int n){
int max = −1000, secMax = −1000, thirdMax = −1000;
int min = 1000, secMin = 1000;
for (int i = 0; i < n; i++){
if (arr[i] > max){
thirdMax = secMax;
secMax = max;
max = arr[i];
}
else if (arr[i] > secMax){
thirdMax = secMax;
secMax = arr[i];
}
else if (arr[i] > thirdMax)
thirdMax = arr[i];
if (arr[i] < min){
secMin = min;
min = arr[i];
}
else if(arr[i] < secMin)
secMin = arr[i];
}
int triplet1 = max * secMax * thirdMax;
int triplet2 = max * secMin * min;
if(triplet1 > triplet2)
return triplet1;
return triplet2;
}
int main(){
int arr[] = { 9, 5, 2, 11, 7, 4 };
int n = sizeof(arr) / sizeof(arr);
cout<<"Maximum product of a triplet in array is
"<<calcMaxProd(arr, n);
return 0;
}

## Output

Maximum product of a triplet in array is 693