Count the number of unordered triplets with elements in increasing order and product less than or equal to integer X
Last Updated :
09 Aug, 2021
Given an array A[] and an integer X. Find the number of unordered triplets (i, j, k) such that A[i] < A[j] < A[k] and A[i] * A[j] * A[k] <= X.
Examples:
Input: A = [3, 2, 5, 7], X = 42
Output: 2
Explanation:
Triplets are :
- (1, 0, 2) => 2 < 3 < 5, 2 * 3 * 5 < = 42
- (1, 0, 3) => 2 < 3 < 7, 2 * 3 * 7 < = 42
Input: A = [3, 1, 2, 56, 21, 8], X = 49
Output: 5
Naive Approach:
The naive method to solve the above-mentioned problem is to iterate through all the triplets. For each triplet arrange them in ascending order (since we have to count unordered triplets, therefore rearranging them is allowed), and check the given condition. But this method takes O(N 3) time.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countTriplets( int a[], int n, int x)
{
int answer = 0;
for ( int i = 0; i < n; i++) {
for ( int j = i + 1; j < n; j++) {
for ( int k = j + 1; k < n; k++) {
vector< int > temp;
temp.push_back(a[i]);
temp.push_back(a[j]);
temp.push_back(a[k]);
sort(temp.begin(), temp.end());
if (temp[0] < temp[1] && temp[1] < temp[2]
&& temp[0] * temp[1] * temp[2] <= x)
answer++;
}
}
}
return answer;
}
int main()
{
int A[] = { 3, 2, 5, 7 };
int N = sizeof (A) / sizeof (A[0]);
int X = 42;
cout << countTriplets(A, N, X);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int countTriplets( int a[], int n, int x)
{
int answer = 0 ;
for ( int i = 0 ; i < n; i++)
{
for ( int j = i + 1 ; j < n; j++)
{
for ( int k = j + 1 ; k < n; k++)
{
Vector<Integer> temp = new Vector<>();
temp.add(a[i]);
temp.add(a[j]);
temp.add(a[k]);
Collections.sort(temp);
if (temp.get( 0 ) < temp.get( 1 ) &&
temp.get( 1 ) < temp.get( 2 ) &&
temp.get( 0 ) * temp.get( 1 ) *
temp.get( 2 ) <= x)
answer++;
}
}
}
return answer;
}
public static void main(String[] args)
{
int A[] = { 3 , 2 , 5 , 7 };
int N = A.length;
int X = 42 ;
System.out.println(countTriplets(A, N, X));
}
}
|
Python3
def countTriplets(a, n, x):
answer = 0
for i in range (n):
for j in range (i + 1 , n):
for k in range (j + 1 , n):
temp = []
temp.append(a[i])
temp.append(a[j])
temp.append(a[k])
temp.sort()
if (temp[ 0 ] < temp[ 1 ] and
temp[ 1 ] < temp[ 2 ] and
temp[ 0 ] * temp[ 1 ] * temp[ 2 ] < = x):
answer + = 1
return answer
A = [ 3 , 2 , 5 , 7 ]
N = len (A)
X = 42
print (countTriplets(A, N, X))
|
C#
using System;
class GFG{
static int countTriplets( int []a, int n, int x)
{
int answer = 0;
for ( int i = 0; i < n; i++)
{
for ( int j = i + 1; j < n; j++)
{
for ( int k = j + 1; k < n; k++)
{
int []temp = { a[i], a[j], a[k] };
Array.Sort(temp);
if (temp[0] < temp[1] &&
temp[1] < temp[2] &&
temp[0] * temp[1] *
temp[2] <= x)
answer++;
}
}
}
return answer;
}
public static void Main()
{
int []A = { 3, 2, 5, 7 };
int N = A.Length;
int X = 42;
Console.WriteLine(countTriplets(A, N, X));
}
}
|
Javascript
<script>
function countTriplets(a, n, x)
{
var answer = 0;
for ( var i = 0; i < n; i++) {
for ( var j = i + 1; j < n; j++) {
for ( var k = j + 1; k < n; k++) {
var temp = [];
temp.push(a[i]);
temp.push(a[j]);
temp.push(a[k]);
temp.sort((a,b)=>a-b)
if (temp[0] < temp[1] && temp[1] < temp[2]
&& temp[0] * temp[1] * temp[2] <= x)
answer++;
}
}
}
return answer;
}
var A = [3, 2, 5, 7];
var N = A.length;
var X = 42;
document.write( countTriplets(A, N, X));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Efficient Approach:
To optimize the method given above we can use a sorted form of the array since it would not change the answer because the triplets are unordered. Traverse through all the pairs of elements in the sorted array. For a pair (p, q) the problem now reduces to finding the number of elements r in the sorted array such that r <= X/(p*q). To perform this efficiently we will use Binary Search method and find the position of the largest element in the array which is < = X/(p*q). All the elements between the index of q until position will be added to the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countTriplets( int a[], int n, int x)
{
int answer = 0;
sort(a, a + n);
for ( int i = 0; i < n; i++) {
for ( int j = i + 1; j < n; j++) {
long long limit = ( long long )x / a[i];
limit = limit / a[j];
int pos = upper_bound(a, a + n, limit) - a;
if (pos > j)
answer = answer + (pos - j - 1);
}
}
return answer;
}
int main()
{
int A[] = { 3, 2, 5, 7 };
int N = sizeof (A) / sizeof (A[0]);
int X = 42;
cout << countTriplets(A, N, X);
return 0;
}
|
Java
import java.io.*;
import java.util.Arrays;
class GFG{
static int countTriplets( int a[], int n, int x)
{
int answer = 0 ;
Arrays.sort(a);
for ( int i = 0 ; i < n; i++)
{
for ( int j = i + 1 ; j < n; j++)
{
int limit = x / a[i];
limit = limit / a[j];
int pos = Arrays.binarySearch(a, limit) + 1 ;
if (pos > j)
answer = answer + (pos - j - 1 );
}
}
return answer;
}
public static void main (String[] args)
{
int A[] = { 3 , 2 , 5 , 7 };
int N = A.length;
int X = 42 ;
System.out.print(countTriplets(A, N, X));
}
}
|
Python3
import bisect
def countTriplets(a, n, x):
answer = 0
a.sort()
for i in range (n):
for j in range (i + 1 , n):
limit = x / a[i]
limit = limit / a[j]
pos = bisect.bisect_right(a, limit)
if (pos > j):
answer = answer + (pos - j - 1 )
return answer
A = [ 3 , 2 , 5 , 7 ]
N = len (A)
X = 42
print (countTriplets(A, N, X))
|
C#
using System;
class GFG{
static int countTriplets( int []a, int n, int x)
{
int answer = 0;
Array.Sort(a);
for ( int i = 0; i < n; i++)
{
for ( int j = i + 1; j < n; j++)
{
int limit = x / a[i];
limit = limit / a[j];
int pos = Array.BinarySearch(a, limit) + 1;
if (pos > j)
answer = answer + (pos - j - 1);
}
}
return answer;
}
public static void Main (String[] args)
{
int []A = { 3, 2, 5, 7 };
int N = A.Length;
int X = 42;
Console.Write(countTriplets(A, N, X));
}
}
|
Javascript
<script>
function countTriplets(a, n, x)
{
var answer = 0;
a.sort((a, b) => a - b)
for ( var i = 0; i < n; i++)
{
for ( var j = i + 1; j < n; j++)
{
var limit = parseInt(x / a[i]);
limit = parseInt(limit / a[j]);
var pos = 0;
for ( var k = a.length - 1; k >= 0; k--)
{
if (a[k] == limit)
{
pos = k;
break ;
}
}
pos += 1
if (pos > j)
answer = answer + (pos - j - 1);
}
}
return answer;
}
var A = [ 3, 2, 5, 7 ];
var N = A.length;
var X = 42;
document.write(countTriplets(A, N, X));
</script>
|
Time Complexity: O(N2 * log(N))
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...