Open In App
Related Articles

Count ways to form minimum product triplets

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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 5C3.
Input : 6
1 3 3 1 3 2
Output : 1
There is only one way (1, 1, 2).

Following cases arise in this problem.  

  1. All three minimum elements are same. For example {1, 1, 1, 1, 2, 3, 4}. The solution for such cases is nC3.
  2. 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 minimum 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 nC2.
  3. All three elements are distinct. For example {1, 2, 3, 3, 5}. In this case, answer is count of occurrences of third element (or nC1).

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.

Implementation:

C++

<div id="highlighter_13225" class="syntaxhighlighter nogutter  "><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="comments">// CPP program to count number of ways we can</code></div><div class="line number2 index1 alt1"><code class="comments">// form triplets with minimum product.</code></div><div class="line number3 index2 alt2"><code class="preprocessor">#include <bits/stdc++.h></code></div><div class="line number4 index3 alt1"><code class="keyword bold">using</code> <code class="keyword bold">namespace</code> <code class="plain">std;</code></div><div class="line number5 index4 alt2"> </div><div class="line number6 index5 alt1"><code class="comments">// function to calculate number of triples</code></div><div class="line number7 index6 alt2"><code class="color1 bold">long</code> <code class="color1 bold">long</code> <code class="plain">noOfTriples(</code><code class="color1 bold">long</code> <code class="color1 bold">long</code> <code class="plain">arr[], </code><code class="color1 bold">int</code> <code class="plain">n)</code></div><div class="line number8 index7 alt1"><code class="plain">{</code></div><div class="line number9 index8 alt2"><code class="undefined spaces">    </code><code class="comments">// Sort the array</code></div><div class="line number10 index9 alt1"><code class="undefined spaces">    </code><code class="plain">sort(arr, arr + n);</code></div><div class="line number11 index10 alt2"> </div><div class="line number12 index11 alt1"><code class="undefined spaces">    </code><code class="comments">// Count occurrences of third element</code></div><div class="line number13 index12 alt2"><code class="undefined spaces">    </code><code class="color1 bold">long</code> <code class="color1 bold">long</code> <code class="plain">count = 0;</code></div><div class="line number14 index13 alt1"><code class="undefined spaces">    </code><code class="keyword bold">for</code> <code class="plain">(</code><code class="color1 bold">long</code> <code class="color1 bold">long</code> <code class="plain">i = 0; i < n; i++) </code></div><div class="line number15 index14 alt2"><code class="undefined spaces">        </code><code class="keyword bold">if</code> <code class="plain">(arr[i] == arr[2])</code></div><div class="line number16 index15 alt1"><code class="undefined spaces">            </code><code class="plain">count++;</code></div><div class="line number17 index16 alt2"><code class="undefined spaces">    </code> </div><div class="line number18 index17 alt1"><code class="undefined spaces">    </code><code class="comments">// If all three elements are same (minimum</code></div><div class="line number19 index18 alt2"><code class="undefined spaces">    </code><code class="comments">// element appears at least 3 times). Answer</code></div><div class="line number20 index19 alt1"><code class="undefined spaces">    </code><code class="comments">// is nC3.</code></div><div class="line number21 index20 alt2"><code class="undefined spaces">    </code><code class="keyword bold">if</code> <code class="plain">(arr[0] == arr[2])</code></div><div class="line number22 index21 alt1"><code class="undefined spaces">        </code><code class="keyword bold">return</code> <code class="plain">(count - 2) * (count - 1) * (count) / 6;</code></div><div class="line number23 index22 alt2"> </div><div class="line number24 index23 alt1"><code class="undefined spaces">    </code><code class="comments">// If minimum element appears once.  </code></div><div class="line number25 index24 alt2"><code class="undefined spaces">    </code><code class="comments">// Answer is nC2.</code></div><div class="line number26 index25 alt1"><code class="undefined spaces">    </code><code class="keyword bold">else</code> <code class="keyword bold">if</code> <code class="plain">(arr[1] == arr[2])</code></div><div class="line number27 index26 alt2"><code class="undefined spaces">        </code><code class="keyword bold">return</code> <code class="plain">(count - 1) * (count) / 2;</code></div><div class="line number28 index27 alt1"><code class="undefined spaces"> </code> </div><div class="line number29 index28 alt2"><code class="undefined spaces">    </code><code class="comments">// Minimum two elements are distinct.</code></div><div class="line number30 index29 alt1"><code class="undefined spaces">    </code><code class="comments">// Answer is nC1.</code></div><div class="line number31 index30 alt2"><code class="undefined spaces">    </code><code class="keyword bold">return</code> <code class="plain">count;</code></div><div class="line number32 index31 alt1"><code class="plain">}</code></div><div class="line number33 index32 alt2"> </div><div class="line number34 index33 alt1"><code class="comments">// Driver code</code></div><div class="line number35 index34 alt2"><code class="color1 bold">int</code> <code class="plain">main()</code></div><div class="line number36 index35 alt1"><code class="plain">{</code></div><div class="line number37 index36 alt2"><code class="undefined spaces">    </code><code class="color1 bold">long</code> <code class="color1 bold">long</code> <code class="plain">arr[] = { 1, 3, 3, 4 };</code></div><div class="line number38 index37 alt1"><code class="undefined spaces">    </code><code class="color1 bold">int</code> <code class="plain">n = </code><code class="keyword bold">sizeof</code><code class="plain">(arr) / </code><code class="keyword bold">sizeof</code><code class="plain">(arr[0]);</code></div><div class="line number39 index38 alt2"><code class="undefined spaces">    </code><code class="plain">cout << noOfTriples(arr, n);</code></div><div class="line number40 index39 alt1"><code class="undefined spaces">    </code><code class="keyword bold">return</code> <code class="plain">0;</code></div><div class="line number41 index40 alt2"><code class="plain">}</code></div></div></td></tr></tbody></table></div>

                    

Java


                    

Python3


                    

C#


                    

Javascript


                    

PHP


                    

Output
1







Time Complexity: O(n Log n) 
Auxiliary Space: O(1)

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)

Approach Using Dp :

C++


                    

Java


                    

Python3

def no_of_triples(arr, n):
    N = 1005  # Define the value of N
 
    # Sorting the array
    arr.sort()
 
    # Initializing a 2D DP array with zeros
    dp = [[0 for _ in range(N)] for _ in range(n)]
    dp[0][arr[0]] = 1
 
    for i in range(1, n):
        dp[i][arr[i]] = 1
         
        # Filling in the DP array for values between arr[i-1] and arr[i]
        for j in range(arr[i - 1] + 1, arr[i]):
            dp[i][j] = dp[i - 1][j]
         
        if arr[i] == arr[1]:
            # Handling special case when arr[i] equals arr[1]
            for j in range(arr[1], arr[2] + 1):
                dp[i][arr[i]] += dp[i - 1][j] * (i - 1) * (i - 2) // 2
        elif arr[i] == arr[2]:
            # Handling special case when arr[i] equals arr[2]
            dp[i][arr[i]] = dp[i - 1][arr[i]] * (i - 1) * (i - 2) // 6
            for j in range(arr[2], N):
                dp[i][j] += dp[i - 1][j]
        else:
            # Filling in the DP array for values greater than arr[2]
            for j in range(arr[2], N):
                dp[i][j] += dp[i - 1][j]
     
    ans = 0
    # Summing up the values in the last row for indices between arr[0] and arr[2]
    for j in range(arr[0], arr[2] + 1):
        ans += dp[n - 1][j]
     
    return ans
 
if __name__ == "__main__":
    arr = [1, 3, 3, 4]
    n = len(arr)
    print(no_of_triples(arr, n))
 
 
# This code is contributed by rambabuguphka

                    

C#


                    

Javascript


                    

Output
1







Time Complexity: O(n Log n) 
Auxiliary Space: O(1)

 



Last Updated : 04 Oct, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads