Open In App

Maximum product of a triplet (subsequence of size 3) in array

Last Updated : 12 Feb, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Given an integer array, find a maximum product of a triplet in the array.

Examples: 

Input:  arr[ ] = [10, 3, 5, 6, 20]
Output: 1200
Explanation: Multiplication of 10, 6 and 20

Input: arr[ ] =  [-10, -3, -5, -6, -20]
Output: -90

Input: arr[ ] =  [1, -4, 3, -6, 7, 0]
Output: 168

[Naive Approach] By Using three nested loops 

A simple solution is to check for every triplet using three nested loops.

C++
// A C++ program to find a maximum product of a
// triplet in array of integers using nestd loops
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

// Function to find a maximum product of a triplet
//   in array of integers of size n
int maxProduct(vector<int> arr)
{
    int n = arr.size();

    int maxProduct = -1e9;

    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++)
                maxProduct = max(maxProduct, arr[i] * arr[j] * arr[k]);

    return maxProduct;
}

int main()
{
    vector<int> arr = {10, 3, 5, 6, 20};
    cout << maxProduct(arr) << endl;
}
Java Python C# JavaScript

Output
1200

Time Complexity: O(n3)
Auxiliary Space: O(1)

[Better Approach] By Using sorting – Time O(n*log(n)) and Space O(1)

  • Sort the array using some efficient in-place sorting algorithm in ascending order.
  • In triplets, either there will be 2 negative elements and 1 positive element or all 3 positive elements so that resultant product will be positive.
  • Therefore, To maximise the result return the maximum of product of the last three elements of the array and the product of the first two elements and last element.
C++
// A C++ program to find a maximum product of a
// triplet in array of integers using sorting
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

/* Function to find a maximum product of a triplet
   in array of integers of size n */
int maxProduct(vector<int>arr)
{
    int n=arr.size();
    
    // Sort the array in ascending order
    sort(arr.begin(), arr.end());

    // Return the maximum of product of last three
    // elements and product of first two elements
    // and last element
    return max(arr[0] * arr[1] * arr[n - 1],
    arr[n - 1] * arr[n - 2] * arr[n - 3]);
}

int main()
{
    vector<int>arr = {-10, -3, 5, 6, -20};
    int max = maxProduct(arr);
    cout<<max<<endl;

    return 0;
}
Java Python C# JavaScript

Output
1200

[Expected Approach] By Using Greedy approach – Time O(n) and Space O(1)

  • Scan the array and compute the Maximum, second maximum and third maximum element present in the array.
  • Scan the array and compute Minimum and second minimum element present in the array.
  • Return the maximum of product of Maximum, second maximum and third maximum and product of Minimum, second minimum and Maximum element.

Note: Step 1 and Step 2 can be done in a single traversal of the array.

C++
// A O(n) C++ program to find maximum product pair in an array.
#include <iostream>
#include <vector>
#include <climits>

using namespace std;

/* Function to find a maximum product of a triplet
   in array of integers of size n */
int maxProduct(vector<int> &arr)
{
    int n = arr.size();
    
    // Initialize Maximum, second maximum and third
    // maximum element
    int maxA = INT_MIN, maxB = INT_MIN, maxC = INT_MIN;

    // Initialize Minimum and second minimum element
    int minA = INT_MAX, minB = INT_MAX;

    for (int i = 0; i < n; i++)
    {
        // Update Maximum, second maximum and third maximum element
        if (arr[i] > maxA)
        {
            maxC = maxB;
            maxB = maxA;
            maxA = arr[i];
        }
        // Update second maximum and third maximum element
        else if (arr[i] > maxB)
        {
            maxC = maxB;
            maxB = arr[i];
        }
        // Update third maximum element
        else if (arr[i] > maxC)
            maxC = arr[i];

        // Update Minimum and second minimum element
        if (arr[i] < minA)
        {
            minB = minA;
            minA = arr[i];
        }
        // Update second minimum element
        else if (arr[i] < minB)
            minB = arr[i];
    }

    return max(minA * minB * maxA, maxA * maxB * maxC);
}

int main()
{
    vector<int>arr = {-10, -3, 5, 6, -20};

    cout<<maxProduct(arr)<<endl;
    
    return 0;
}
Java Python C# JavaScript

Output
1200


 



Next Article
Article Tags :
Practice Tags :

Similar Reads

three90RightbarBannerImg