Skip to content
Related Articles

Related Articles

Improve Article

Count triplets (i, j, k) in an array of distinct elements such that a[i] < a[j] > a[k] and i < j < k

  • Last Updated : 12 Apr, 2021

Given an array arr[] consisting of N distinct integers, the task is to count the number of triplets (i, j, k) possible from the array arr[] such that i < j < k and arr[i] < arr[j] > arr[k].

Examples:

Input: arr[] = {2, 3, 1, -1}
Output: 2
Explanation: From the given array, all possible triplets satisfying the property (i, j, k) and arr[i] < arr[j] > arr[k] are:

  1. (0, 1, 2): arr[0](= 2) < arr[1](= 3) > arr[2](= 1).
  2. (0, 1, 3): arr[0](= 2) < arr[1](= 3) > arr[3](= -1).

Therefore, the count of triplets is 2.

Input: arr[] = {2, 3, 4, 6, 7, 9, 1, 12, 10, 8}
Output: 41



Naive Approach: The simplest approach to solve the problem is to traverse the given array and for each element arr[i], the product of the count of smaller elements on the left side of arr[i] and the count of smaller elements on the right side of arr[i] gives the count of triplets for the element arr[i] as the middle element. The sum of all the counts obtained for each index is the required number of valid triplets.
Time Complexity: O(N2)
Auxiliary Space: O(1)

Efficient Approach: The above approach can also be optimized by finding the count of smaller elements using a Policy-based data structure (PBDS). Follow the steps below to solve the problem:

  • Initialize the variable, say ans to 0 that stores the total number of possible pairs.
  • Initialize two containers of the Policy-based data structure, say P and Q.
  • Initialize a vector of pairs V, where V[i]. first and V[i].second stores the count of smaller elements on the left and the right side of every array element arr[i].
  • Traverse the given array and for each element arr[i], update the value of V[i].first as P.order_of_key(arr[i]) and insert arr[i] to set P.
  • Traverse the array from right to left and for each element arr[i], update the value of V[i].first as P.order_of_key(arr[i]) and insert arr[i] to set Q.
  • Traverse the vector of pairs V and add the value of V[i].first * V[i].second to the variable ans.
  • After completing the above steps, print the value of ans as the total number of pairs.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
  
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
#include <iostream>
using namespace __gnu_pbds;
using namespace std;
  
// Function to find the count of triplets
// satisfying the given conditions
void findTriplets(int arr[], int n)
{
    // Stores the total count of pairs
    int ans = 0;
  
    // Declare the set
    tree<int, null_type, less<int>, rb_tree_tag,
         tree_order_statistics_node_update>
        p, q;
  
    // Declare the vector of pairs
    vector<pair<int, int> > v(n);
  
    // Iterate over the array from
    // left to right
    for (int i = 0; i < n; i++) {
  
        // Find the index of element
        // in sorted array
        int index = p.order_of_key(arr[i]);
  
        // Assign to the left
        v[i].first = index;
  
        // Insert into the set
        p.insert(arr[i]);
    }
  
    // Iterate from right to left
    for (int i = n - 1; i >= 0; i--) {
  
        // Find the index of element
        // in the sorted array
        int index = q.order_of_key(arr[i]);
  
        // Assign to the right
        v[i].second = index;
  
        // Insert into the set
        q.insert(arr[i]);
    }
  
    // Traverse the vector of pairs
    for (int i = 0; i < n; i++) {
        ans += (v[i].first * v[i].second);
    }
  
    // Print the total count
    cout << ans;
}
  
// Driver Code
int main()
{
    int arr[] = { 2, 3, 1, -1 };
    int N = sizeof(arr) / sizeof(arr[0]);
    findTriplets(arr, N);
  
    return 0;
}


Output:

2

Time Complexity: O(N * log N)
Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :