Related Articles

# Count number of triplets with product equal to given number | Set 2

• Difficulty Level : Medium
• Last Updated : 06 May, 2021

Given an array of distinct integers(considering only positive numbers) and a number ‘m’, find the number of triplets with the product equal to ‘m’.
Examples:

```Input: arr[] = { 1, 4, 6, 2, 3, 8}
m = 24
Output: 3

Input: arr[] = { 0, 4, 6, 2, 3, 8}
m = 18
Output: 0```

An approach with O(n) extra space has already been discussed in previous post. In this post an approach with O(1) space complexity will be discussed.
Approach: The idea is to use Three-pointer technique:

1. Sort the input array.
2. Fix the first element as A[i] where i is from 0 to array size – 2.
3. After fixing the first element of triplet, find the other two elements using 2 pointer technique.

Below is the implementation of above approach:

## C++

 `// C++ implementation of above approach``#include ``using` `namespace` `std;` `// Function to count such triplets``int` `countTriplets(``int` `arr[], ``int` `n, ``int` `m)``{``    ``int` `count = 0;` `    ``// Sort the array``    ``sort(arr, arr + n);``    ``int` `end, start, mid;` `    ``// three pointer technique``    ``for` `(end = n - 1; end >= 2; end--) {``        ``int` `start = 0, mid = end - 1;``        ``while` `(start < mid) {` `            ``// Calculate the product of a triplet``            ``long` `int` `prod = arr[end] * arr[start] * arr[mid];` `            ``// Check if that product is greater than m,``            ``// decrement mid``            ``if` `(prod > m)``                ``mid--;` `            ``// Check if that product is smaller than m,``            ``// increment start``            ``else` `if` `(prod < m)``                ``start++;` `            ``// Check if that product is equal to m,``            ``// decrement mid, increment start and``            ``// increment the count of pairs``            ``else` `if` `(prod == m) {``                ``count++;``                ``mid--;``                ``start++;``            ``}``        ``}``    ``}` `    ``return` `count;``}` `// Drivers code``int` `main()``{``    ``int` `arr[] = { 1, 1, 1, 1, 1, 1 };``    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr);``    ``int` `m = 1;` `    ``cout << countTriplets(arr, n, m);` `    ``return` `0;``}`

## Java

 `// Java implementation of``// above approach``import` `java.io.*;``import` `java.util.*;` `class` `GFG``{` `// Function to count such triplets``static` `int` `countTriplets(``int` `arr[],``                         ``int` `n, ``int` `m)``{``    ``int` `count = ``0``;` `    ``// Sort the array``    ``Arrays.sort(arr);``    ``int` `end, start, mid;` `    ``// three pointer technique``    ``for` `(end = n - ``1``; end >= ``2``; end--)``    ``{``        ``start = ``0``; mid = end - ``1``;``        ``while` `(start < mid)``        ``{` `            ``// Calculate the product``            ``// of a triplet``            ``long` `prod = arr[end] *``                        ``arr[start] *``                        ``arr[mid];` `            ``// Check if that product``            ``// is greater than m,``            ``// decrement mid``            ``if` `(prod > m)``                ``mid--;` `            ``// Check if that product``            ``// is smaller than m,``            ``// increment start``            ``else` `if` `(prod < m)``                ``start++;` `            ``// Check if that product is equal``            ``// to m, decrement mid, increment``            ``// start and increment the``            ``// count of pairs``            ``else` `if` `(prod == m)``            ``{``                ``count++;``                ``mid--;``                ``start++;``            ``}``        ``}``    ``}` `    ``return` `count;``}` `// Driver code``public` `static` `void` `main (String[] args)``{``    ``int` `[]arr = { ``1``, ``1``, ``1``, ``1``, ``1``, ``1` `};``    ``int` `n = arr.length;``    ``int` `m = ``1``;``    ` `    ``System.out.println(countTriplets(arr, n, m));``}``}` `// This code is contributed``// by inder_verma.`

## Python3

 `# Python3 implementation``# of above approach` `# Function to count such triplets``def` `countTriplets(arr, n, m):` `    ``count ``=` `0` `    ``# Sort the array``    ``arr.sort()` `    ``# three pointer technique``    ``for` `end ``in` `range``(n ``-` `1``, ``1``, ``-``1``) :``        ``start ``=` `0``        ``mid ``=` `end ``-` `1``        ``while` `(start < mid) :` `            ``# Calculate the product``            ``# of a triplet``            ``prod ``=` `(arr[end] ``*``                    ``arr[start] ``*` `arr[mid])` `            ``# Check if that product is``            ``# greater than m, decrement mid``            ``if` `(prod > m):``                ``mid ``-``=` `1` `            ``# Check if that product is``            ``# smaller than m, increment start``            ``elif` `(prod < m):``                ``start ``+``=` `1` `            ``# Check if that product is equal``            ``# to m, decrement mid, increment``            ``# start and increment the count``            ``# of pairs``            ``elif` `(prod ``=``=` `m):``                ``count ``+``=` `1``                ``mid ``-``=` `1``                ``start ``+``=` `1` `    ``return` `count` `# Drivers code``if` `__name__ ``=``=` `"__main__"``:``    ``arr ``=` `[ ``1``, ``1``, ``1``, ``1``, ``1``, ``1` `]``    ``n ``=` `len``(arr)``    ``m ``=` `1` `    ``print``(countTriplets(arr, n, m))` `# This code is contributed``# by ChitraNayal`

## C#

 `// C# implementation of above approach``using` `System;` `class` `GFG``{` `// Function to count such triplets``static` `int` `countTriplets(``int` `[]arr,``                         ``int` `n, ``int` `m)``{``    ``int` `count = 0;` `    ``// Sort the array``    ``Array.Sort(arr);``    ``int` `end, start, mid;` `    ``// three pointer technique``    ``for` `(end = n - 1; end >= 2; end--)``    ``{``        ``start = 0; mid = end - 1;``        ``while` `(start < mid)``        ``{` `            ``// Calculate the product``            ``// of a triplet``            ``long` `prod = arr[end] *``                        ``arr[start] *``                        ``arr[mid];` `            ``// Check if that product``            ``// is greater than m,``            ``// decrement mid``            ``if` `(prod > m)``                ``mid--;` `            ``// Check if that product``            ``// is smaller than m,``            ``// increment start``            ``else` `if` `(prod < m)``                ``start++;` `            ``// Check if that product``            ``// is equal to m,``            ``// decrement mid, increment``            ``// start and increment the``            ``// count of pairs``            ``else` `if` `(prod == m)``            ``{``                ``count++;``                ``mid--;``                ``start++;``            ``}``        ``}``    ``}` `    ``return` `count;``}` `// Driver code``public` `static` `void` `Main (String []args)``{``    ``int` `[]arr = { 1, 1, 1, 1, 1, 1 };``    ``int` `n = arr.Length;``    ``int` `m = 1;``    ` `    ``Console.WriteLine(countTriplets(arr, n, m));``}``}` `// This code is contributed``// by Arnab Kundu`

## PHP

 `= 2; ``\$end``--) {``         ``\$start` `= 0;``         ``\$mid` `= ``\$end` `- 1;``        ``while` `(``\$start` `< ``\$mid``) {` `            ``// Calculate the product of a triplet``            ``\$prod` `= ``\$arr``[``\$end``] * ``\$arr``[``\$start``] * ``\$arr``[``\$mid``];` `            ``// Check if that product is greater than m,``            ``// decrement mid``            ``if` `(``\$prod` `> ``\$m``)``                ``\$mid``--;` `            ``// Check if that product is smaller than m,``            ``// increment start``            ``else` `if` `(``\$prod` `< ``\$m``)``                ``\$start``++;` `            ``// Check if that product is equal to m,``            ``// decrement mid, increment start and``            ``// increment the count of pairs``            ``else` `if` `(``\$prod` `== ``\$m``) {``                ``\$count``++;``                ``\$mid``--;``                ``\$start``++;``            ``}``        ``}``    ``}` `    ``return` `\$count``;``}` `// Drivers code`` ` `    ``\$arr` `= ``array``( 1, 1, 1, 1, 1, 1 );``     ``\$n` `= sizeof(``\$arr``) / sizeof(``\$arr``);``    ``\$m` `= 1;` `    ``echo`  `countTriplets(``\$arr``, ``\$n``, ``\$m``);`  `#This Code is Contributed by ajit``?>`

## Javascript

 ``
Output:
`6`

Time complexity: O(N^2)
Space Complexity: O(1)

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up