Given an integer array, find the minimum product among all combinations of triplets in the array.

For example,

Input:  { 4, -1, 3, 5, 9 }
Output: The minimum product is -45 (-1, 5, 9)
 
 
Input:  { 1, 4, 10, -2, 4 }
Output: The minimum product is -80 (10, 4, -2)
 
 
Input:  { 3, 4, 1, 2, 5 }
Output: The minimum product is 6 (3, 1, 2)

Practice this problem

A naive solution would be to consider every combination of triplets present in the array and calculate their product. The idea is to maintain the maximum product found so far in a variable and update it if the product of the current triplet is greater. The implementation can be seen here and runs in O(n3) time, where n is the size of the input.

1. Using Sorting

A better approach, that takes O(n.log(n)) time, is to sort the array and return the minimum among its first three elements and the product of the first element with its last two items. This logic works as the multiplication of two negative numbers results in a positive number. The multiplication of a large positive number with a negative number results in a large negative number.

Following is the implementation of the above approach in C++, Java, and Python:

C++


Download  Run Code

Output:

The minimum product is -45

Java


Download  Run Code

Output:

The minimum product is -45

Python


Download  Run Code

Output:

The minimum product is -45

2. Linear time solution

The following approach runs in O(n) time but takes O(n) extra space. The idea is to take the help of four auxiliary arrays, left_min[], left_max[], right_min[], and right_max[] of the same size as the input array, where:

  • left_min[i] contains the minimum element to the left of A[i].
  • left_max[i] contains the maximum element to the left of A[i].
  • right_min[i] contains the minimum element to the right of A[i].
  • right_max[i] contains the maximum element to the right of A[i].

 
After the arrays’ construction, we can get minimum and maximum elements on the left or right side for any array index in constant time. So, consider every array element as the middle element of the triplet, except the first and last element, and find the minimum by considering all possible combinations. This approach is demonstrated below in C, Java, and Python:

C


Download  Run Code

Output:

The minimum product is -45

Java


Download  Run Code

Output:

The minimum product is -45

Python


Download  Run Code

Output:

The minimum product is -45

3. Linear time and constant space solution

We have seen that the sorting solution only uses the first three and last two array elements but sorts the whole array, which modifies the input array and is also costly for large inputs. This can be avoided by finding the smallest, the second smallest, and the third smallest element, along with the largest and the second largest array element in linear time. Then like the sorting solution, return the minimum among the product of the smallest, second smallest, third smallest elements in the array and product of the smallest, largest, second largest elements in the array.

Following is the C, Java, and Python program that demonstrates it:

C


Download  Run Code

Output:

The minimum product is -45

Java


Download  Run Code

Output:

The minimum product is -45

Python


Download  Run Code

Output:

The minimum product is -45

 
Follow up:

Find a triplet having the maximum product in an array