Count the triplets such that A[i] < B[j] < C[k]

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++
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;Â
// Function to return the count// of elements in arr[] which are// less than the given keyint countLessThan(int arr[], int n, int key){Â Â Â Â int l = 0, r = n - 1;Â Â Â Â int index = -1;Â
    // Modified binary search    while (l <= r) {        int m = (l + r) / 2;Â
        if (arr[m] < key) {            l = m + 1;            index = m;        }        else {            r = m - 1;        }    }Â
    return (index + 1);}Â
// Function to return the count// of elements in arr[] which are// greater than the given keyint countGreaterThan(int arr[], int n, int key){Â Â Â Â int l = 0, r = n - 1;Â Â Â Â int index = -1;Â
    // Modified binary search    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);}Â
// Function to return the count// of the required tripletsint countTriplets(int n, int* a, int* b, int* c){    // Sort all three arrays    sort(a, a + n);    sort(b, b + n);    sort(c, c + n);Â
    int count = 0;Â
    // Iterate for all the elements of array B    for (int i = 0; i < n; ++i) {        int current = b[i];        int a_index = -1, c_index = -1;Â
        // Count of elements in A[]        // which are less than the        // chosen element from B[]        int low = countLessThan(a, n, current);Â
        // Count of elements in C[]        // which are greater than the        // chosen element from B[]        int high = countGreaterThan(c, n, current);Â
        // Update the count        count += (low * high);    }Â
    return count;}Â
// Driver codeint 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
// Java implementation of the approach import java.util.*;Â
class GFG { Â
    // Function to return the count     // of elements in arr[] which are     // less than the given key     static int countLessThan(int arr[], int n, int key)     {         int l = 0, r = n - 1;         int index = -1;              // Modified binary search         while (l <= r)         {             int m = (l + r) / 2;                  if (arr[m] < key)             {                 l = m + 1;                 index = m;             }             else            {                 r = m - 1;             }         }              return (index + 1);     }          // Function to return the count     // of elements in arr[] which are     // greater than the given key     static int countGreaterThan(int arr[], int n, int key)     {         int l = 0, r = n - 1;         int index = -1;              // Modified binary search         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);     }          // Function to return the count     // of the required triplets     static int countTriplets(int n, int a[], int b[], int c[])     {         // Sort all three arrays         Arrays.sort(a) ;         Arrays.sort(b);         Arrays.sort(c);              int count = 0;              // Iterate for all the elements of array B         for (int i = 0; i < n; ++i)         {             int current = b[i];                           // Count of elements in A[]             // which are less than the             // chosen element from B[]             int low = countLessThan(a, n, current);                  // Count of elements in C[]             // which are greater than the             // chosen element from B[]             int high = countGreaterThan(c, n, current);                  // Update the count             count += (low * high);         }              return count;     }          // Driver code     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));          } } // This code is contributed by Arnab Kundu |
Python 3
# Python 3 implementation of the approachÂ
# Function to return the count# of elements in arr[] which are# less than the given keydef countLessThan(arr, n, key):Â Â Â Â l = 0Â Â Â Â r = n - 1Â Â Â Â index = -1Â
    # Modified binary search    while (l <= r):        m = (l + r) // 2Â
        if (arr[m] < key) :            l = m + 1            index = m                 else :            r = m - 1             return (index + 1)Â
# Function to return the count# of elements in arr[] which are# greater than the given keydef countGreaterThan(arr, n, key):Â
    l = 0    r = n - 1    index = -1Â
    # Modified binary search    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)Â
Â
# Function to return the count# of the required tripletsdef countTriplets(n, a, b, c):Â
    # Sort all three arrays    a.sort    b.sort()    c.sort()Â
    count = 0Â
    # Iterate for all the elements of array B    for i in range(n):        current = b[i]        a_index = -1        c_index = -1Â
        # Count of elements in A[]        # which are less than the        # chosen element from B[]        low = countLessThan(a, n, current)Â
        # Count of elements in C[]        # which are greater than the        # chosen element from B[]        high = countGreaterThan(c, n, current)Â
        # Update the count        count += (low * high)Â
    return countÂ
Â
# Driver codeif __name__ == "__main__":Â
    a = [ 1, 5 ]    b = [ 2, 4 ]    c = [ 3, 6 ]    size = len(a)Â
    print( countTriplets(size, a, b, c))     # This code is contributed by ChitraNayal |
C#
// C# implementation of the approach using System;Â
class GFG{Â
    // Function to return the count     // of elements in arr[] which are     // less than the given key     static int countLessThan(int []arr, int n, int key)     {         int l = 0, r = n - 1;         int index = -1;              // Modified binary search         while (l <= r)        {             int m = (l + r) / 2;                  if (arr[m] < key)             {                 l = m + 1;                 index = m;             }             else            {                 r = m - 1;             }         }              return (index + 1);     }          // Function to return the count     // of elements in arr[] which are     // greater than the given key     static int countGreaterThan(int []arr, int n, int key)     {         int l = 0, r = n - 1;         int index = -1;              // Modified binary search         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);     }          // Function to return the count     // of the required triplets     static int countTriplets(int n, int []a, int []b, int []c)     {         // Sort all three arrays         Array.Sort(a) ;         Array.Sort(b);         Array.Sort(c);              int count = 0;              // Iterate for all the elements of array B         for (int i = 0; i < n; ++i)        {             int current = b[i];                           // Count of elements in A[]             // which are less than the             // chosen element from B[]             int low = countLessThan(a, n, current);                  // Count of elements in C[]             // which are greater than the             // chosen element from B[]             int high = countGreaterThan(c, n, current);                  // Update the count             count += (low * high);         }              return count;     }          // Driver code     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));          } }Â
// This code is contributed by AnkitRai01 |
PHP
<?php// PHP implementation of the approachÂ
// Function to return the count// of elements in arr[] which are// less than the given keyfunction countLessThan(&$arr, $n, $key){Â Â Â Â $l = 0;Â Â Â Â $r = $n - 1;Â Â Â Â $index = -1;Â
    // Modified binary search    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 to return the count// of elements in arr[] which are// greater than the given keyfunction countGreaterThan(&$arr, $n, $key){Â Â Â Â $l = 0;Â Â Â Â $r = $n - 1;Â Â Â Â $index = -1;Â
    // Modified binary search    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 to return the count// of the required tripletsfunction countTriplets($n, &$a, &$b, &$c){    // Sort all three arrays    sort($a);    sort($b);    sort($c);Â
    $count = 0;Â
    // Iterate for all the elements of array B    for ($i = 0; $i < $n; ++$i)     {        $current = $b[$i];        $a_index = -1;        $c_index = -1;Â
        // Count of elements in A[]        // which are less than the        // chosen element from B[]        $low = countLessThan($a, $n, $current);Â
        // Count of elements in C[]        // which are greater than the        // chosen element from B[]        $high = countGreaterThan($c, $n, $current);Â
        // Update the count        $count += ($low * $high);    }Â
    return $count;}Â
// Driver code$a = array( 1, 5 );$b = array( 2, 4 );$c = array( 3, 6 );$size = sizeof($a);Â
echo countTriplets($size, $a, $b, $c);Â
// This code is contributed by ChitraNayal?> |
Javascript
<script>Â
// JavaScript implementation of the approach Â
Â
// Function to return the count     // of elements in arr[] which are     // less than the given key function countLessThan(arr,n,key){    let l = 0, r = n - 1;         let index = -1;                // Modified binary search         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 to return the count     // of elements in arr[] which are     // greater than the given key function countGreaterThan(arr,n,key){    let l = 0, r = n - 1;         let index = -1;                // Modified binary search         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 to return the count     // of the required triplets function countTriplets(n,a,b,c){    // Sort all three arrays         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;                // Iterate for all the elements of array B         for (let i = 0; i < n; ++i)         {             let current = b[i];                               // Count of elements in A[]             // which are less than the             // chosen element from B[]             let low = countLessThan(a, n, current);                    // Count of elements in C[]             // which are greater than the             // chosen element from B[]             let high = countGreaterThan(c, n, current);                    // Update the count             count += (low * high);         }                return count; }Â
// Driver code let a=[1, 5 ];let b=[2, 4];let c=[3, 6 ];let size = a.length;document.write(countTriplets(size, a, b, c)); Â Â Â Â Â Â
// This code is contributed by avanitrachhadiya2155Â
</script> |
3
Â
Time Complexity: O(nlog(n))
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



