Given an array of distinct integers(considering only positive numbers) and a number ‘m’, find the number of triplets with product equal to ‘m’.
Examples:
Input : arr[] = { 1, 4, 6, 2, 3, 8}
m = 24
Output : 3
{1, 4, 6} {1, 3, 8} {4, 2, 3}
Input : arr[] = { 0, 4, 6, 2, 3, 8}
m = 18
Output : 0
Asked in : Microsoft
A Naive approach is to consider each and every triplet one by one and count if their product is equal to m.
Implementation:
C++
#include <iostream>
using namespace std;
int countTriplets( int arr[], int n, int m)
{
int count = 0;
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++)
if (arr[i] * arr[j] * arr[k] == m)
count++;
return count;
}
int main()
{
int arr[] = { 1, 4, 6, 2, 3, 8 };
int n = sizeof (arr) / sizeof (arr[0]);
int m = 24;
cout << countTriplets(arr, n, m);
return 0;
}
|
Java
class GFG {
static int countTriplets( int arr[], int n, int m)
{
int count = 0 ;
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++)
if (arr[i] * arr[j] * arr[k] == m)
count++;
return count;
}
public static void main(String[] args)
{
int arr[] = { 1 , 4 , 6 , 2 , 3 , 8 };
int m = 24 ;
System.out.println(countTriplets(arr, arr.length, m));
}
}
|
Python3
def countTriplets(arr, n, m):
count = 0
for i in range (n - 2 ):
for j in range (i + 1 , n - 1 ):
for k in range (j + 1 , n):
if (arr[i] * arr[j] * arr[k] = = m):
count + = 1
return count
if __name__ = = "__main__" :
arr = [ 1 , 4 , 6 , 2 , 3 , 8 ]
m = 24
print (countTriplets(arr,
len (arr), m))
|
C#
using System;
public class GFG {
static int countTriplets( int [] arr, int n, int m)
{
int count = 0;
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++)
if (arr[i] * arr[j] * arr[k] == m)
count++;
return count;
}
public static void Main()
{
int [] arr = { 1, 4, 6, 2, 3, 8 };
int m = 24;
Console.WriteLine(countTriplets(arr, arr.Length, m));
}
}
|
PHP
<?php
function countTriplets( $arr , $n , $m )
{
$count = 0;
for ( $i = 0; $i < $n - 2; $i ++)
for ( $j = $i + 1; $j < $n - 1; $j ++)
for ( $k = $j + 1; $k < $n ; $k ++)
if ( $arr [ $i ] * $arr [ $j ] * $arr [ $k ] == $m )
$count ++;
return $count ;
}
$arr = array (1, 4, 6, 2, 3, 8);
$n = sizeof( $arr );
$m = 24;
echo countTriplets( $arr , $n , $m );
?>
|
Javascript
<script>
function countTriplets(arr,n,m)
{
let count = 0;
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++)
if (arr[i] * arr[j] * arr[k] == m)
count++;
return count;
}
let arr = [ 1, 4, 6, 2, 3, 8];
let m = 24;
document.write(countTriplets(arr, arr.length, m));
</script>
|
Time Complexity: O(n3)
Auxiliary Space: O(1)
An Efficient Method is to use Hashing.
- Store all the elements in a hash_map with their index.
- Consider all pairs(i, j) and check the following:
- If (arr[i]*arr[j] !=0 && (m % arr[i]*arr[j]) == 0), If yes, then search for ( m / (arr[i]*arr[j]) in the map.
- Also check m / (arr[i]*arr[j]) is not equal to arr[i] and arr[j].
- Also, check that the current triplet is not counted previously by using the index stored in the map.
- If all the above conditions are satisfied, then increment the count.
- Return count.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int countTriplets( int arr[], int n, int m)
{
unordered_map< int , int > occ;
for ( int i = 0; i < n; i++)
occ[arr[i]] = i;
int count = 0;
for ( int i = 0; i < n - 1; i++) {
for ( int j = i + 1; j < n; j++) {
if ((arr[i] * arr[j] <= m) && (arr[i] * arr[j] != 0) && (m % (arr[i] * arr[j]) == 0)) {
int check = m / (arr[i] * arr[j]);
auto it = occ.find(check);
if (check != arr[i] && check != arr[j]
&& it != occ.end() && it->second > i
&& it->second > j)
count++;
}
}
}
return count;
}
int main()
{
int arr[] = { 1, 4, 6, 2, 3, 8 };
int n = sizeof (arr) / sizeof (arr[0]);
int m = 24;
cout << countTriplets(arr, n, m);
return 0;
}
|
Java
import java.util.HashMap;
class GFG {
static int countTriplets( int arr[], int n, int m)
{
HashMap<Integer, Integer> occ = new HashMap<Integer, Integer>(n);
for ( int i = 0 ; i < n; i++)
occ.put(arr[i], i);
int count = 0 ;
for ( int i = 0 ; i < n - 1 ; i++) {
for ( int j = i + 1 ; j < n; j++) {
if ((arr[i] * arr[j] <= m) && (arr[i] * arr[j] != 0 ) && (m % (arr[i] * arr[j]) == 0 )) {
int check = m / (arr[i] * arr[j]);
occ.containsKey(check);
if (check != arr[i] && check != arr[j]
&& occ.containsKey(check) && occ.get(check) > i
&& occ.get(check) > j)
count++;
}
}
}
return count;
}
public static void main(String[] args)
{
int arr[] = { 1 , 4 , 6 , 2 , 3 , 8 };
int m = 24 ;
System.out.println(countTriplets(arr, arr.length, m));
}
}
|
Python3
def countTriplets(li,product):
flag = 0
count = 0
for i in range ( len (li)):
if li[i]! = 0 and product % li[i] = = 0 :
for j in range (i + 1 , len (li)):
if li[j]! = 0 and product % (li[j] * li[i]) = = 0 :
if product / / (li[j] * li[i]) in li:
n = li.index(product / / (li[j] * li[i]))
if n > i and n > j:
flag = 1
count + = 1
print (count)
li = [ 1 , 4 , 6 , 2 , 3 , 8 ]
product = 24
countTriplets(li,product)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int countTriplets( int [] arr,
int n, int m)
{
Dictionary< int ,
int > occ = new Dictionary< int ,
int >(n);
for ( int i = 0; i < n; i++)
occ.Add(arr[i], i);
int count = 0;
for ( int i = 0; i < n - 1; i++)
{
for ( int j = i + 1; j < n; j++)
{
if ((arr[i] * arr[j] <= m) &&
(arr[i] * arr[j] != 0) &&
(m % (arr[i] * arr[j]) == 0))
{
int check = m / (arr[i] * arr[j]);
if (check != arr[i] &&
check != arr[j] &&
occ.ContainsKey(check) &&
occ[check] > i &&
occ[check] > j)
count++;
}
}
}
return count;
}
static void Main()
{
int [] arr = {1, 4, 6,
2, 3, 8};
int m = 24;
Console.WriteLine(countTriplets(arr,
arr.Length, m));
}
}
|
Javascript
<script>
function countTriplets(arr, n, m)
{
var occ = new Map();
for ( var i = 0; i < n; i++)
{
occ.set(arr[i], i)
}
var count = 0;
for ( var i = 0; i < n - 1; i++)
{
for ( var j = i + 1; j < n; j++)
{
if ((arr[i] * arr[j] <= m) && (arr[i] * arr[j] != 0) && (m % (arr[i] * arr[j]) == 0)) {
var check = parseInt(m / (arr[i] * arr[j]));
var ff = occ.has(check);
var ans;
if (ff)
ans = occ.get(check)
if (check != arr[i] && check != arr[j]
&& ff && ans > i
&& ans > j)
count++;
}
}
}
return count;
}
var arr = [1, 4, 6, 2, 3, 8];
var n = arr.length;
var m = 24;
document.write( countTriplets(arr, n, m));
</script>
|
Time Complexity : O(n2)
Auxiliary Space : O(n)
New Approach:- Another approach to solve this problem is to sort the array and then use two pointers to find the triplets whose product is equal to the given number.
Steps:-
- Define a function named countTriplets that takes an integer array, the size of the array, and an integer m as arguments.
- Sort the input array in ascending order using the built-in sort() function of C++.
- Initialize a variable named count to 0.
- For each element i in the array from index 0 to n-3:
*Set two pointers, left and right, to the next and last index of the remaining elements, respectively.
*While the left pointer is less than the right pointer:
*If the product of the element i, the element at the left pointer, and the element at the right pointer is equal to m, then increment the count and move the left pointer to the right and the right pointer to the left.
*Else, if the product is less than m, then move the left pointer to the right.
*Else, if the product is greater than m, then move the right pointer to the left. - Return the value of count.
- Define the main function.
- Declare an integer array named arr and initialize it with some values.
- Calculate the size of the array and store it in an integer variable named n.
- Define an integer variable named m and initialize it with a value of 24.
- Call the countTriplets function with the arguments arr, n, and m and print the returned value.
Here’s the implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int countTriplets( int arr[], int n, int m)
{
sort(arr, arr + n);
int count = 0;
for ( int i = 0; i < n - 2; i++) {
int left = i + 1, right = n - 1;
while (left < right) {
if (arr[i] * arr[left] * arr[right] == m) {
count++;
left++;
right--;
}
else if (arr[i] * arr[left] * arr[right] < m)
left++;
else
right--;
}
}
return count;
}
int main()
{
int arr[] = { 1, 4, 6, 2, 3, 8 };
int n = sizeof (arr) / sizeof (arr[0]);
int m = 24;
cout << countTriplets(arr, n, m);
return 0;
}
|
Java
import java.util.Arrays;
public class CountTriplets {
public static int countTriplets( int arr[], int n, int m) {
Arrays.sort(arr);
int count = 0 ;
for ( int i = 0 ; i < n - 2 ; i++) {
int left = i + 1 , right = n - 1 ;
while (left < right) {
if (arr[i] * arr[left] * arr[right] == m) {
count++;
left++;
right--;
} else if (arr[i] * arr[left] * arr[right] < m)
left++;
else
right--;
}
}
return count;
}
public static void main(String[] args) {
int arr[] = { 1 , 4 , 6 , 2 , 3 , 8 };
int n = arr.length;
int m = 24 ;
System.out.println(countTriplets(arr, n, m));
}
}
|
Python3
def countTriplets(arr, n, m):
arr.sort()
count = 0
for i in range (n - 2 ):
left = i + 1
right = n - 1
while left < right:
if arr[i] * arr[left] * arr[right] = = m:
count + = 1
left + = 1
right - = 1
elif arr[i] * arr[left] * arr[right] < m:
left + = 1
else :
right - = 1
return count
arr = [ 1 , 4 , 6 , 2 , 3 , 8 ]
n = len (arr)
m = 24
print (countTriplets(arr, n, m))
|
C#
using System;
public class Program
{
static int CountTriplets( int [] arr, int n, int m)
{
Array.Sort(arr);
int count = 0;
for ( int i = 0; i < n - 2; i++)
{
int left = i + 1, right = n - 1;
while (left < right)
{
if (arr[i] * arr[left] * arr[right] == m)
{
count++;
left++;
right--;
}
else if (arr[i] * arr[left] * arr[right] < m)
left++;
else
right--;
}
}
return count;
}
static void Main()
{
int [] arr = { 1, 4, 6, 2, 3, 8 };
int n = arr.Length;
int m = 24;
Console.WriteLine(CountTriplets(arr, n, m));
}
}
|
Javascript
function countTriplets(arr, n, m) {
arr.sort((a, b) => a - b);
let count = 0;
for (let i = 0; i < n - 2; i++) {
let left = i + 1, right = n - 1;
while (left < right) {
if (arr[i] * arr[left] * arr[right] === m) {
count++;
left++;
right--;
} else if (arr[i] * arr[left] * arr[right] < m)
left++;
else
right--;
}
}
return count;
}
const arr = [1, 4, 6, 2, 3, 8];
const n = arr.length;
const m = 24;
console.log(countTriplets(arr, n, m));
|
Time Complexity:- The time complexity of this approach is O(n^2), which is better than the naive approach but worse than the hashing approach. However, this approach has the advantage of not requiring any extra space for storing the elements.
Auxiliary Space:- The auxiliary space of this code is O(1) because the space used is constant and does not depend on the size of the input array or the value of m. The only additional space used is for the variables i, left, right, and count, which require a constant amount of space regardless of the input. The sorting is done in-place, so it does not require any additional space. Therefore, the space complexity of this code is O(1).
This article is contributed by Sahil Chhabra. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.