Given an array of positive integers. We need to find how many triples of indices (i, j, k) (i < j < k), such that a[i] * a[j] * a[k] is minimum possible.

Examples:Input : 5 1 3 2 3 4 Output : 2 The triplets are (1, 3, 2) and (1, 2, 3) Input : 5 2 2 2 2 2 Output : 5 In this example we choose three 2s out of five, and the number of ways to choose them is^{5}C_{3}. Input : 6 1 3 3 1 3 2 Output : 1 There is only one way (1, 1, 2).

Following cases arise in this problem.

- All three minimum elements are same. For example {1, 1, 1, 1, 2, 3, 4}. The solution for such cases is
^{n}C_{3}. - Two elements are same. For example {1, 2, 2, 2, 3} or {1, 1, 2, 2}. In this case, count of occurrences of first (or minimum element) cannot be more than 2. If minimuum element appears two times, then answer is count of second element (We get to choose only 1 from all occurrences of second element. If minimum element appears once, the count is
^{n}C_{2}. - All three elements are distinct. For example {1, 2, 3, 3, 5}. In this case, answer is count of occurrences of third element (or
^{n}C_{1}).

We first sort the array in increasing order. Then count the frequency of 3 element of 3rd element from starting. Let the frequency be ‘count’. Following cases arise.

- If 3rd element is equal to the first element, no. of triples will be (count-2)*(count-1)*(count)/6, where count is the frequency of 3rd element.
- If 3rd element is equal to 2nd element, no. of triples will be (count-1)*(count)/2. Otherwise no. of triples will be value of count.

## C++

`// CPP program to count number of ways we can ` `// form triplets with minimum product. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to calculate number of triples ` `long` `long` `noOfTriples(` `long` `long` `arr[], ` `int` `n) ` `{ ` ` ` `// Sort the array ` ` ` `sort(arr, arr + n); ` ` ` ` ` `// Count occurrences of third element ` ` ` `long` `long` `count = 0; ` ` ` `for` `(` `long` `long` `i = 0; i < n; i++) ` ` ` `if` `(arr[i] == arr[2]) ` ` ` `count++; ` ` ` ` ` `// If all three elements are same (minimum ` ` ` `// element appears at least 3 times). Answer ` ` ` `// is nC3. ` ` ` `if` `(arr[0] == arr[2]) ` ` ` `return` `(count - 2) * (count - 1) * (count) / 6; ` ` ` ` ` `// If minimum element appears once. ` ` ` `// Answer is nC2. ` ` ` `else` `if` `(arr[1] == arr[2]) ` ` ` `return` `(count - 1) * (count) / 2; ` ` ` ` ` `// Minimum two elements are distinct. ` ` ` `// Answer is nC1. ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `long` `long` `arr[] = { 1, 3, 3, 4 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `cout << noOfTriples(arr, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count number of ways we can ` `// form triplets with minimum product. ` `import` `java.util.Arrays; ` ` ` `class` `GFG { ` ` ` ` ` `// function to calculate number of triples ` ` ` `static` `long` `noOfTriples(` `long` `arr[], ` `int` `n) ` ` ` `{ ` ` ` ` ` `// Sort the array ` ` ` `Arrays.sort(arr); ` ` ` ` ` `// Count occurrences of third element ` ` ` `long` `count = ` `0` `; ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `if` `(arr[i] == arr[` `2` `]) ` ` ` `count++; ` ` ` ` ` `// If all three elements are same (minimum ` ` ` `// element appears at least 3 times). Answer ` ` ` `// is nC3. ` ` ` `if` `(arr[` `0` `] == arr[` `2` `]) ` ` ` `return` `(count - ` `2` `) * (count - ` `1` `) * ` ` ` `(count) / ` `6` `; ` ` ` ` ` `// If minimum element appears once. ` ` ` `// Answer is nC2. ` ` ` `else` `if` `(arr[` `1` `] == arr[` `2` `]) ` ` ` `return` `(count - ` `1` `) * (count) / ` `2` `; ` ` ` ` ` `// Minimum two elements are distinct. ` ` ` `// Answer is nC1. ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `//driver code ` ` ` `public` `static` `void` `main(String arg[]) ` ` ` `{ ` ` ` ` ` `long` `arr[] = { ` `1` `, ` `3` `, ` `3` `, ` `4` `}; ` ` ` `int` `n = arr.length; ` ` ` ` ` `System.out.print(noOfTriples(arr, n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to count number ` `# of ways we can form triplets ` `# with minimum product. ` ` ` `# function to calculate number of triples ` `def` `noOfTriples (arr, n): ` ` ` ` ` `# Sort the array ` ` ` `arr.sort() ` ` ` ` ` `# Count occurrences of third element ` ` ` `count ` `=` `0` ` ` `for` `i ` `in` `range` `(n): ` ` ` `if` `arr[i] ` `=` `=` `arr[` `2` `]: ` ` ` `count` `+` `=` `1` ` ` ` ` `# If all three elements are same ` ` ` `# (minimum element appears at l ` ` ` `# east 3 times). Answer is nC3. ` ` ` `if` `arr[` `0` `] ` `=` `=` `arr[` `2` `]: ` ` ` `return` `(count ` `-` `2` `) ` `*` `(count ` `-` `1` `) ` `*` `(count) ` `/` `6` ` ` ` ` `# If minimum element appears once. ` ` ` `# Answer is nC2. ` ` ` `elif` `arr[` `1` `] ` `=` `=` `arr[` `2` `]: ` ` ` `return` `(count ` `-` `1` `) ` `*` `(count) ` `/` `2` ` ` ` ` `# Minimum two elements are distinct. ` ` ` `# Answer is nC1. ` ` ` `return` `count ` ` ` `# Driver code ` `arr ` `=` `[` `1` `, ` `3` `, ` `3` `, ` `4` `] ` `n ` `=` `len` `(arr) ` `print` `(noOfTriples(arr, n)) ` ` ` `# This code is contributed by "Abhishek Sharma 44" ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count number of ways ` `// we can form triplets with minimum product. ` `using` `System; ` ` ` `class` `GFG { ` ` ` `// function to calculate number of triples ` `static` `long` `noOfTriples(` `long` `[]arr, ` `int` `n) ` `{ ` ` ` `// Sort the array ` ` ` `Array.Sort(arr); ` ` ` ` ` `// Count occurrences of third element ` ` ` `long` `count = 0; ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `if` `(arr[i] == arr[2]) ` ` ` `count++; ` ` ` ` ` `// If all three elements are same (minimum ` ` ` `// element appears at least 3 times). Answer ` ` ` `// is nC3. ` ` ` `if` `(arr[0] == arr[2]) ` ` ` `return` `(count - 2) * (count - 1) * (count) / 6; ` ` ` ` ` `// If minimum element appears once. ` ` ` `// Answer is nC2. ` ` ` `else` `if` `(arr[1] == arr[2]) ` ` ` `return` `(count - 1) * (count) / 2; ` ` ` ` ` `// Minimum two elements are distinct. ` ` ` `// Answer is nC1. ` ` ` `return` `count; ` `} ` ` ` `//driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `long` `[]arr = { 1, 3, 3, 4 }; ` ` ` `int` `n = arr.Length; ` ` ` `Console.Write(noOfTriples(arr, n)); ` `} ` `} ` ` ` `//This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to count number of ways ` `// we can form triplets with minimum ` `// product. ` ` ` `// function to calculate number of ` `// triples ` `function` `noOfTriples( ` `$arr` `, ` `$n` `) ` `{ ` ` ` `// Sort the array ` ` ` `sort(` `$arr` `); ` ` ` ` ` `// Count occurrences of third element ` ` ` `$count` `= 0; ` ` ` `for` `( ` `$i` `= 0; ` `$i` `< ` `$n` `; ` `$i` `++) ` ` ` `if` `(` `$arr` `[` `$i` `] == ` `$arr` `[2]) ` ` ` `$count` `++; ` ` ` ` ` `// If all three elements are same ` ` ` `// (minimum element appears at least ` ` ` `// 3 times). Answer is nC3. ` ` ` `if` `(` `$arr` `[0] == ` `$arr` `[2]) ` ` ` `return` `(` `$count` `- 2) * (` `$count` `- 1) ` ` ` `* (` `$count` `) / 6; ` ` ` ` ` `// If minimum element appears once. ` ` ` `// Answer is nC2. ` ` ` `else` `if` `(` `$arr` `[1] == ` `$arr` `[2]) ` ` ` `return` `(` `$count` `- 1) * (` `$count` `) / 2; ` ` ` ` ` `// Minimum two elements are distinct. ` ` ` `// Answer is nC1. ` ` ` `return` `$count` `; ` `} ` ` ` `// Driver code ` ` ` `$arr` `= ` `array` `( 1, 3, 3, 4 ); ` ` ` `$n` `= ` `count` `(` `$arr` `); ` ` ` `echo` `noOfTriples(` `$arr` `, ` `$n` `); ` ` ` `// This code is contributed by anuj_67. ` `?> ` |

*chevron_right*

*filter_none*

Output:

1

Time Complexity: O(n Log n)

The solution can be optimized by first finding minimum element and its frequency and if frequency is less than 3, then finding second minimum and its frequency. If overall frequency is less than 3, then finding third minimum and its frequency. Time complexity of this optimized solution would be O(n)

This article is contributed by **Sagar Shukla**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count the number of unordered triplets with elements in increasing order and product less than or equal to integer X
- Count of ways to generate a Matrix with product of each row and column as 1 or -1
- Count number of triplets with product equal to given number with duplicates allowed
- Count number of triplets with product equal to given number with duplicates allowed | Set-2
- Count number of triplets with product equal to given number | Set 2
- Count number of triplets with product equal to given number
- Ways to form n/2 pairs such that difference of pairs is minimum
- Minimum count of increment of K size subarrays required to form a given Array
- Print all triplets in sorted array that form AP
- Count the triplets such that A[i] < B[j] < C[k]
- Count of triplets in an Array (i, j, k) such that i < j < k and a[k] < a[i] < a[j]
- Count triplets in an array such that i<j<k and a[j] - a[i] = a[k] - a[j] = D
- Count of triplets in a given Array having GCD K
- Count of triplets in an Array such that A[i] * A[j] = A[k] and i < j < k
- Count triplets with sum smaller than a given value
- Count of triplets that satisfy the given equation
- Count triplets such that sum of any two number is equal to third | Set 2
- Count Triplets such that one of the numbers can be written as sum of the other two
- Count of triplets in an array that satisfy the given conditions
- Count of ordered triplets (R, G, B) in a given original string