Given an array A[ ] consisting of N positive integers, the task is to find the number of triplets A[i], A[j] & A[k] in the array such that i < j < k and A[i] * A[j] = A[k].
Input: N = 5, A[ ] = {2, 3, 4, 6, 12}
Output: 3
Explanation:
The valid triplets from the given array are:
(A[0], A[1], A[3]) = (2, 3, 6) where (2*3 = 6)
(A[0], A[3], A[4]) = (2, 6, 12) where (2*6 = 12)
(A[1], A[2], A[4]) = (3, 4, 12) where (3*4 = 12)
Hence, a total of 3 triplets exists which satisfies the given condition.
Input: N = 3, A[ ] = {1, 1, 1}
Output: 1
Explanation:
The only valid triplet is (A[0], A[1], A[2]) = (1, 1, 1)
Naive Approach:
The simplest approach to solve the problem is to generate all possible triplets and for each triplet, check if it satisfies the required condition. If found to be true, increase the count of triplets. After complete traversal of the array and generating all possible triplets, print the final count.
C++
#include <bits/stdc++.h>
using namespace std;
int countTriplets( int A[], int N)
{
int ans = 0;
for ( int i = 0; i < N; i++) {
for ( int j = i + 1; j < N; j++) {
for ( int k = j + 1; k < N; k++) {
if (A[i] * A[j] == A[k])
ans++;
}
}
}
return ans;
}
int main()
{
int N = 5;
int A[] = { 2, 3, 4, 6, 12 };
cout << countTriplets(A, N);
return 0;
}
|
Java
import java.io.*;
public class GFG {
static int countTriplets( int A[], int N)
{
int ans = 0 ;
for ( int i = 0 ; i < N; i++) {
for ( int j = i + 1 ; j < N; j++) {
for ( int k = j + 1 ; k < N; k++) {
if (A[i] * A[j] == A[k])
ans++;
}
}
}
return ans;
}
public static void main (String[] args) {
int N = 5 ;
int A[] = { 2 , 3 , 4 , 6 , 12 };
System.out.println(countTriplets(A, N));
}
}
|
Python3
def countTriplets( A, N):
ans = 0 ;
for i in range ( 0 , N):
for j in range (i + 1 , N):
for k in range (j + 1 , N):
if (A[i] * A[j] = = A[k]):
ans + = 1 ;
return ans;
N = 5 ;
A = [ 2 , 3 , 4 , 6 , 12 ];
print (countTriplets(A, N));
|
C#
using System;
public class GFG
{
public static int countTriplets( int [] A, int N)
{
var ans = 0;
for ( int i = 0; i < N; i++)
{
for ( int j = i + 1; j < N; j++)
{
for ( int k = j + 1; k < N; k++)
{
if (A[i] * A[j] == A[k])
{
ans++;
}
}
}
}
return ans;
}
public static void Main(String[] args)
{
var N = 5;
int [] A = {2, 3, 4, 6, 12};
Console.WriteLine(countTriplets(A, N));
}
}
|
Javascript
function countTriplets(A, N)
{
let ans = 0;
for (let i = 0; i < N; i++) {
for (let j = i + 1; j < N; j++) {
for (let k = j + 1; k < N; k++) {
if (A[i] * A[j] == A[k])
ans++;
}
}
}
return ans;
}
let N = 5;
let A = [ 2, 3, 4, 6, 12 ];
console.log(countTriplets(A, N));
|
Time Complexity: O(N3)
Auxiliary Space: O(1)
Efficient Approach:
The above approach can be optimized using Two Pointers and HashMap.
Follow the steps below to solve the problem:
- Initialize a Map to store frequencies of array elements.
- Iterate over the array in reverse, i.e. loop with a variable j in the range [N – 2, 1].
- For every j, increase the count of A[j + 1] in the map. Iterate over the range [0, j – 1] using variable i and check if A[i] * A[j] is present in the map or not.
- If A[i] * A[j] is found in the map, increase the count of triplets by the frequency of A[i] * A[j] stored in the map.
- After complete traversal of the array, print the final count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countTriplets( int A[], int N)
{
int ans = 0;
map< int , int > map;
for ( int j = N - 2; j >= 1; j--) {
map[A[j + 1]]++;
for ( int i = 0; i < j; i++) {
int target = A[i] * A[j];
if (map.find(target)
!= map.end())
ans += map[target];
}
}
return ans;
}
int main()
{
int N = 5;
int A[] = { 2, 3, 4, 6, 12 };
cout << countTriplets(A, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int countTriplets( int A[], int N)
{
int ans = 0 ;
HashMap<Integer,
Integer> map = new HashMap<Integer,
Integer>();
for ( int j = N - 2 ; j >= 1 ; j--)
{
if (map.containsKey(A[j + 1 ]))
map.put(A[j + 1 ], map.get(A[j + 1 ]) + 1 );
else
map.put(A[j + 1 ], 1 );
for ( int i = 0 ; i < j; i++)
{
int target = A[i] * A[j];
if (map.containsKey(target))
ans += map.get(target);
}
}
return ans;
}
public static void main(String[] args)
{
int N = 5 ;
int A[] = { 2 , 3 , 4 , 6 , 12 };
System.out.print(countTriplets(A, N));
}
}
|
Python3
from collections import defaultdict
def countTriplets(A, N):
ans = 0
map = defaultdict( lambda : 0 )
for j in range (N - 2 , 0 , - 1 ):
map [A[j + 1 ]] + = 1
for i in range (j):
target = A[i] * A[j]
if (target in map .keys()):
ans + = map [target]
return ans
if __name__ = = '__main__' :
N = 5
A = [ 2 , 3 , 4 , 6 , 12 ]
print (countTriplets(A, N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int countTriplets( int []A, int N)
{
int ans = 0;
Dictionary< int ,
int > map = new Dictionary< int ,
int >();
for ( int j = N - 2; j >= 1; j--)
{
if (map.ContainsKey(A[j + 1]))
map[A[j + 1]] = map[A[j + 1]] + 1;
else
map.Add(A[j + 1], 1);
for ( int i = 0; i < j; i++)
{
int target = A[i] * A[j];
if (map.ContainsKey(target))
ans += map[target];
}
}
return ans;
}
public static void Main(String[] args)
{
int N = 5;
int []A = { 2, 3, 4, 6, 12 };
Console.Write(countTriplets(A, N));
}
}
|
Javascript
<script>
function countTriplets(A, N)
{
let ans = 0;
let map = new Map();
for (let j = N - 2; j >= 1; j--)
{
if (map.has(A[j + 1]))
map.set(A[j + 1], map.get(A[j + 1]) + 1);
else
map.set(A[j + 1], 1);
for (let i = 0; i < j; i++)
{
let target = A[i] * A[j];
if (map.has(target))
ans += map.get(target);
}
}
return ans;
}
let N = 5;
let A = [ 2, 3, 4, 6, 12 ];
document.write(countTriplets(A, N));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(N)
Contact Us