Maximum product of a triplet (subsequence of size 3) in array
Last Updated :
12 Feb, 2025
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
// 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
# 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#
// 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
// 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));
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
// 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);
}
}
Python
# 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#
// 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
// 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);
[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
// 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));
}
}
Python
# 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))
C#
// 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));
}
}
JavaScript
// 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));
Similar Reads
Maximum product of an increasing subsequence of size 3 Given an array of distinct positive integers, the task is to find the maximum product of increasing subsequence of size 3, i.e., we need to find arr[i]*arr[j]*arr[k] such that arr[i] < arr[j] < arr[k] and i < j < k < n Examples: Input: arr[] = {10, 11, 9, 5, 6, 1, 20}Output: 2200 Expl
14 min read
Maximum product of bitonic subsequence of size 3 Given an array arr[] of positive integers of size N, the task is to find the maximum product of bitonic subsequence of size 3.Bitonic Subsequence: subsequence in which elements are first in the increasing order and then decreasing order. Elements in the subsequence are follow this order arr[i] <
15 min read
Maximum product of subsequence of size k Given an array A[] of n integers, the task is to find a subsequence of size k whose product is maximum among all possible k sized subsequences of the given array. Constraints 1 <= n <= 10^5 1 <= k <= n Examples: Input : A[] = {1, 2, 0, 3}, k = 2 Output : 6 Explanation : Subsequence conta
15 min read
Maximum product quadruple (sub-sequence of size 4) in array Given an integer array, find a maximum product of a quadruple in the array. Examples: Input: [10, 3, 5, 6, 20] Output: 6000 Multiplication of 10, 5, 6 and 20 Input: [-10, -3, -5, -6, -20] Output: 6000 Input: [1, -4, 3, -6, 7, 0] Output: 504 Approach 1 (Naive, O( ) time, O(1) Space) Steps to solve th
15+ min read
Find the Increasing subsequence of length three with maximum product Given a sequence of non-negative integers, find the subsequence of length 3 having maximum product with the numbers of the subsequence being in ascending order. Examples: Input: arr[] = {6, 7, 8, 1, 2, 3, 9, 10} Output: 8 9 10 Input: arr[] = {1, 5, 10, 8, 9} Output: 5 8 9 Approach: Since we want to
10 min read
POTD Solutions | 23 Octâ 23 | Maximum Sum Increasing Subsequence View all POTD Solutions Welcome to the daily solutions of our PROBLEM OF THE DAY (POTD). We will discuss the entire problem step-by-step and work towards developing an optimized solution. This will not only help you brush up on your concepts of Dynamic Programming but will also help you build up pro
4 min read