Count the triplets such that A[i] < B[j] < C[k]
Last Updated :
21 Jun, 2022
Given three array A[], B[] and C[] of N integers each. The task is to find the count of triplets (A[i], B[j], C[k]) such that A[i] < B[j] < C[k].
Input: A[] = {1, 5}, B[] = {2, 4}, C[] = {3, 6}
Output: 3
Triplets are (1, 2, 3), (1, 4, 6) and (1, 2, 6)
Input: A[] = {1, 1, 1}, B[] = {2, 2, 2}, C[] = {3, 3, 3}
Output: 27
Approach: Sort all the given arrays. Now fix an element say X in array B[] and for each X, the answer will be the product of the count of elements in array A[] which are less than X and the count of elements in array C[] which are greater than X. We can compute both of these counts using modified binary search.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countLessThan( int arr[], int n, int key)
{
int l = 0, r = n - 1;
int index = -1;
while (l <= r) {
int m = (l + r) / 2;
if (arr[m] < key) {
l = m + 1;
index = m;
}
else {
r = m - 1;
}
}
return (index + 1);
}
int countGreaterThan( int arr[], int n, int key)
{
int l = 0, r = n - 1;
int index = -1;
while (l <= r) {
int m = (l + r) / 2;
if (arr[m] <= key) {
l = m + 1;
}
else {
r = m - 1;
index = m;
}
}
if (index == -1)
return 0;
return (n - index);
}
int countTriplets( int n, int * a, int * b, int * c)
{
sort(a, a + n);
sort(b, b + n);
sort(c, c + n);
int count = 0;
for ( int i = 0; i < n; ++i) {
int current = b[i];
int a_index = -1, c_index = -1;
int low = countLessThan(a, n, current);
int high = countGreaterThan(c, n, current);
count += (low * high);
}
return count;
}
int main()
{
int a[] = { 1, 5 };
int b[] = { 2, 4 };
int c[] = { 3, 6 };
int size = sizeof (a) / sizeof (a[0]);
cout << countTriplets(size, a, b, c);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int countLessThan( int arr[], int n, int key)
{
int l = 0 , r = n - 1 ;
int index = - 1 ;
while (l <= r)
{
int m = (l + r) / 2 ;
if (arr[m] < key)
{
l = m + 1 ;
index = m;
}
else
{
r = m - 1 ;
}
}
return (index + 1 );
}
static int countGreaterThan( int arr[], int n, int key)
{
int l = 0 , r = n - 1 ;
int index = - 1 ;
while (l <= r)
{
int m = (l + r) / 2 ;
if (arr[m] <= key)
{
l = m + 1 ;
}
else
{
r = m - 1 ;
index = m;
}
}
if (index == - 1 )
return 0 ;
return (n - index);
}
static int countTriplets( int n, int a[], int b[], int c[])
{
Arrays.sort(a) ;
Arrays.sort(b);
Arrays.sort(c);
int count = 0 ;
for ( int i = 0 ; i < n; ++i)
{
int current = b[i];
int low = countLessThan(a, n, current);
int high = countGreaterThan(c, n, current);
count += (low * high);
}
return count;
}
public static void main(String args[])
{
int a[] = { 1 , 5 };
int b[] = { 2 , 4 };
int c[] = { 3 , 6 };
int size = a.length;
System.out.println(countTriplets(size, a, b, c));
}
}
|
Python 3
def countLessThan(arr, n, key):
l = 0
r = n - 1
index = - 1
while (l < = r):
m = (l + r) / / 2
if (arr[m] < key) :
l = m + 1
index = m
else :
r = m - 1
return (index + 1 )
def countGreaterThan(arr, n, key):
l = 0
r = n - 1
index = - 1
while (l < = r) :
m = (l + r) / / 2
if (arr[m] < = key) :
l = m + 1
else :
r = m - 1
index = m
if (index = = - 1 ):
return 0
return (n - index)
def countTriplets(n, a, b, c):
a.sort
b.sort()
c.sort()
count = 0
for i in range (n):
current = b[i]
a_index = - 1
c_index = - 1
low = countLessThan(a, n, current)
high = countGreaterThan(c, n, current)
count + = (low * high)
return count
if __name__ = = "__main__" :
a = [ 1 , 5 ]
b = [ 2 , 4 ]
c = [ 3 , 6 ]
size = len (a)
print ( countTriplets(size, a, b, c))
|
C#
using System;
class GFG
{
static int countLessThan( int []arr, int n, int key)
{
int l = 0, r = n - 1;
int index = -1;
while (l <= r)
{
int m = (l + r) / 2;
if (arr[m] < key)
{
l = m + 1;
index = m;
}
else
{
r = m - 1;
}
}
return (index + 1);
}
static int countGreaterThan( int []arr, int n, int key)
{
int l = 0, r = n - 1;
int index = -1;
while (l <= r)
{
int m = (l + r) / 2;
if (arr[m] <= key)
{
l = m + 1;
}
else
{
r = m - 1;
index = m;
}
}
if (index == -1)
return 0;
return (n - index);
}
static int countTriplets( int n, int []a, int []b, int []c)
{
Array.Sort(a) ;
Array.Sort(b);
Array.Sort(c);
int count = 0;
for ( int i = 0; i < n; ++i)
{
int current = b[i];
int low = countLessThan(a, n, current);
int high = countGreaterThan(c, n, current);
count += (low * high);
}
return count;
}
public static void Main()
{
int []a = { 1, 5 };
int []b = { 2, 4 };
int []c = { 3, 6 };
int size = a.Length;
Console.WriteLine(countTriplets(size, a, b, c));
}
}
|
PHP
<?php
function countLessThan(& $arr , $n , $key )
{
$l = 0;
$r = $n - 1;
$index = -1;
while ( $l <= $r )
{
$m = intval (( $l + $r ) / 2);
if ( $arr [ $m ] < $key )
{
$l = $m + 1;
$index = $m ;
}
else
{
$r = $m - 1;
}
}
return ( $index + 1);
}
function countGreaterThan(& $arr , $n , $key )
{
$l = 0;
$r = $n - 1;
$index = -1;
while ( $l <= $r )
{
$m = intval (( $l + $r ) / 2);
if ( $arr [ $m ] <= $key )
{
$l = $m + 1;
}
else
{
$r = $m - 1;
$index = $m ;
}
}
if ( $index == -1)
return 0;
return ( $n - $index );
}
function countTriplets( $n , & $a , & $b , & $c )
{
sort( $a );
sort( $b );
sort( $c );
$count = 0;
for ( $i = 0; $i < $n ; ++ $i )
{
$current = $b [ $i ];
$a_index = -1;
$c_index = -1;
$low = countLessThan( $a , $n , $current );
$high = countGreaterThan( $c , $n , $current );
$count += ( $low * $high );
}
return $count ;
}
$a = array ( 1, 5 );
$b = array ( 2, 4 );
$c = array ( 3, 6 );
$size = sizeof( $a );
echo countTriplets( $size , $a , $b , $c );
?>
|
Javascript
<script>
function countLessThan(arr,n,key)
{
let l = 0, r = n - 1;
let index = -1;
while (l <= r)
{
let m = Math.floor((l + r) / 2);
if (arr[m] < key)
{
l = m + 1;
index = m;
}
else
{
r = m - 1;
}
}
return (index + 1);
}
function countGreaterThan(arr,n,key)
{
let l = 0, r = n - 1;
let index = -1;
while (l <= r)
{
let m = Math.floor((l + r) / 2);
if (arr[m] <= key)
{
l = m + 1;
}
else
{
r = m - 1;
index = m;
}
}
if (index == -1)
return 0;
return (n - index);
}
function countTriplets(n,a,b,c)
{
a.sort( function (e,f){ return e-f;}) ;
b.sort( function (e,f){ return e-f;}) ;
c.sort( function (e,f){ return e-f;}) ;
let count = 0;
for (let i = 0; i < n; ++i)
{
let current = b[i];
let low = countLessThan(a, n, current);
let high = countGreaterThan(c, n, current);
count += (low * high);
}
return count;
}
let a=[1, 5 ];
let b=[2, 4];
let c=[3, 6 ];
let size = a.length;
document.write(countTriplets(size, a, b, c));
</script>
|
Time Complexity: O(nlog(n))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...