Maximum product of a triplet (subsequence of size 3) in array
Last Updated :
12 Feb, 2025
Improve
Try it on GfG Practice
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 20Input: arr[ ] = [-10, -3, -5, -6, -20]
Output: -90Input: 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.
// 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;
}
// A Java program to find a
// maximum product of a
// triplet in array of integers using nested loops
class GFG {
// Function to find a maximum
// product of a triplet in array
// of integers of size n
static int maxProduct(int[] arr)
{
int n = arr.length;
// will contain max product
int max_product = Integer.MIN_VALUE;
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++)
max_product = Math.max(max_product,
arr[i] * arr[j]
* arr[k]);
return max_product;
}
public static void main(String[] args)
{
int[] arr = { 10, 3, 5, 6, 20 };
int res = maxProduct(arr);
System.out.println(res);
}
}
# Python program to find a maximum product of a
# triplet in array of integers using nestd loops
# Function to find a maximum product of a triplet
# in array of integers of size n
def maxProduct(arr):
n = len(arr)
maxProduct = -10**9
for i in range(n - 2):
for j in range(i + 1, n - 1):
for k in range(j + 1, n):
maxProduct = max(maxProduct, arr[i] * arr[j] * arr[k])
return maxProduct
if __name__ == "__main__":
arr = [10, 3, 5, 6, 20]
print(maxProduct(arr))
// C# program to find a maximum product of a
// triplet in array of integers using nested loops
using System;
using System.Collections.Generic;
class GfG {
// Function to find a maximum product of a triplet
// in array of integers of size n
static int maxProduct(List<int> arr)
{
int n = arr.Count;
int maxProduct = -1000000000;
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 = Math.Max(maxProduct,
arr[i] * arr[j]
* arr[k]);
return maxProduct;
}
static void Main()
{
List<int> arr = new List<int>{ 10, 3, 5, 6, 20 };
Console.WriteLine(maxProduct(arr));
}
}
// JavaScript program to find a maximum product of a
// triplet in array of integers using nested loops
// Function to find a maximum product of a triplet
// in array of integers of size n
function maxProduct(arr)
{
let n = arr.length;
let maxProduct = -1e9;
for (let i = 0; i < n - 2; i++)
for (let j = i + 1; j < n - 1; j++)
for (let k = j + 1; k < n; k++)
maxProduct = Math.max(
maxProduct, arr[i] * arr[j] * arr[k]);
return maxProduct;
}
// Driver code
let arr = [ 10, 3, 5, 6, 20 ];
console.log(maxProduct(arr));
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.
// 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 program to find a maximum product of a
// triplet in array of integers using sorting
import java.util.*;
class GfG {
/* Function to find a maximum product of a triplet
in array of integers of size n */
static int maxProduct(int[] arr) {
int n = arr.length;
// Sort the array in ascending order
Arrays.sort(arr);
// Return the maximum of product of last three
// elements and product of first two elements
// and last element
return Math.max(arr[0] * arr[1] * arr[n - 1],
arr[n - 1] * arr[n - 2] * arr[n - 3]);
}
public static void main(String[] args) {
int[] arr = {-10, -3, 5, 6, -20};
int max = maxProduct(arr);
System.out.println(max);
}
}
# A Python3 program to find a maximum
# product of a triplet in an array of integers
# using sorting
# Function to find a maximum product of a triplet
# in array of integers of size n
def maxProduct(arr):
n = len(arr)
# Sort the array in ascending order
arr.sort()
# 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])
if __name__ == "__main__":
arr = [-10, -3, 5, 6, -20]
max = maxProduct(arr)
print(max)
// C# program to find a maximum product of a
// triplet in array of integers using sorting
using System;
using System.Collections.Generic;
class GfG {
/* Function to find a maximum product of a triplet
in array of integers of size n */
static int maxProduct(List<int> arr) {
int n = arr.Count;
// Sort the array in ascending order
arr.Sort();
// Return the maximum of product of last three
// elements and product of first two elements
// and last element
return Math.Max(arr[0] * arr[1] * arr[n - 1],
arr[n - 1] * arr[n - 2] * arr[n - 3]);
}
static void Main() {
List<int> arr = new List<int> { -10, -3, 5, 6, -20 };
int max = maxProduct(arr);
Console.WriteLine(max);
}
}
// Javascript program to find a maximum
// product of a triplet in array of integers
// using sorting
// Function to find a maximum product of a triplet
// in array of integers of size n
function maxProduct(arr) {
let n = arr.length;
// Sort the array in ascending order
arr.sort((a, b) => a - b);
// Return the maximum of product of last three
// elements and product of first two elements
// and last element
return Math.max(arr[0] * arr[1] * arr[n - 1],
arr[n - 1] * arr[n - 2] * arr[n - 3]);
}
//Driver code
let arr = [-10, -3, 5, 6, -20];
let max = maxProduct(arr);
console.log(max);
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.
// 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;
}
// A O(n) Java program to find maximum product
// pair in an array.
import java.util.*;
class GfG {
/* Function to find a maximum product of a triplet
in array of integers of size n */
static int maxProduct(int[] arr) {
int n = arr.length;
// Initialize Maximum,
// second maximum and third maximum element
int maxA = Integer.MIN_VALUE,
maxB = Integer.MIN_VALUE, maxC = Integer.MIN_VALUE;
// Initialize Minimum and second minimum element
int minA = Integer.MAX_VALUE, minB = Integer.MAX_VALUE;
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];
} else if (arr[i] > maxB) {
maxC = maxB;
maxB = arr[i];
} else if (arr[i] > maxC) {
maxC = arr[i];
}
// Update Minimum and second minimum element
if (arr[i] < minA) {
minB = minA;
minA = arr[i];
} else if (arr[i] < minB) {
minB = arr[i];
}
}
return Math.max(minA * minB * maxA, maxA * maxB * maxC);
}
public static void main(String[] args) {
int[] arr = {-10, -3, 5, 6, -20};
System.out.println(maxProduct(arr));
}
}
# Function to find a maximum product of a triplet
# in array of integers of size n
def maxProduct(arr):
n = len(arr)
# Initialize Maximum, second maximum
# and third maximum element
maxA = float('-inf')
maxB = float('-inf')
maxC = float('-inf')
# Initialize Minimum and second minimum element
minA = float('inf')
minB = float('inf')
for i in range(n):
# Update Maximum, second maximum
#and third maximum element
if arr[i] > maxA:
maxC = maxB
maxB = maxA
maxA = arr[i]
elif arr[i] > maxB:
maxC = maxB
maxB = arr[i]
elif arr[i] > maxC:
maxC = arr[i]
# Update Minimum and second minimum element
if arr[i] < minA:
minB = minA
minA = arr[i]
elif arr[i] < minB:
minB = arr[i]
return max(minA * minB * maxA, maxA * maxB * maxC)
if __name__ == "__main__":
arr = [-10, -3, 5, 6, -20]
print(maxProduct(arr))
// A O(n) C# program to find maximum product
// pair in an array.
using System;
using System.Collections.Generic;
class GfG {
/* Function to find a maximum product of a triplet
in array of integers of size n */
static int maxProduct(List<int> arr) {
int n = arr.Count;
// Initialize Maximum, second
// maximum and third maximum element
int maxA = int.MinValue,
maxB = int.MinValue, maxC = int.MinValue;
// Initialize Minimum and second minimum element
int minA = int.MaxValue, minB = int.MaxValue;
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];
} else if (arr[i] > maxB) {
maxC = maxB;
maxB = arr[i];
} else if (arr[i] > maxC) {
maxC = arr[i];
}
// Update Minimum and second minimum element
if (arr[i] < minA) {
minB = minA;
minA = arr[i];
} else if (arr[i] < minB) {
minB = arr[i];
}
}
return Math.Max(minA * minB * maxA, maxA * maxB * maxC);
}
static void Main() {
List<int> arr = new List<int> { -10, -3, 5, 6, -20 };
Console.WriteLine(maxProduct(arr));
}
}
// A O(n) javascript program to find maximum product
// pair in an array.
// Function to find a maximum product of a triplet
// in array of integers of size n
function maxProduct(arr) {
let n = arr.length;
// Initialize Maximum, second maximum
// and third maximum element
let maxA = Number.MIN_SAFE_INTEGER,
maxB = Number.MIN_SAFE_INTEGER,
maxC = Number.MIN_SAFE_INTEGER;
// Initialize Minimum and second
// minimum element
let minA = Number.MAX_SAFE_INTEGER,
minB = Number.MAX_SAFE_INTEGER;
for (let i = 0; i < n; i++) {
// Update Maximum, second maximum
// and third maximum element
if (arr[i] > maxA) {
maxC = maxB;
maxB = maxA;
maxA = arr[i];
} else if (arr[i] > maxB) {
maxC = maxB;
maxB = arr[i];
} else if (arr[i] > maxC) {
maxC = arr[i];
}
// Update Minimum and second minimum element
if (arr[i] < minA) {
minB = minA;
minA = arr[i];
} else if (arr[i] < minB) {
minB = arr[i];
}
}
return Math.max(minA * minB * maxA, maxA * maxB * maxC);
}
// Driver code
let arr = [-10, -3, 5, 6, -20];
console.log(maxProduct(arr));
Output
1200