Find number of triplets in array such that a[i]>a[j]>a[k] and i<j<k
Given an array arr of size N. The task is to count number of triplets in the array such that a[i]>a[j]>a[k] and i<j<k
Examples:
Input : arr[] = {10, 8, 3, 1}
Output : 4
The triplets are:
1, 3, 8
1, 3, 10
1, 8, 10
3, 8, 10
Input : arr[] = {88, 64, 45, 21, 54}
Output : 5
Prerequisites: Count inversions
Approach:
- Find the greater_left array. greater_left[i] represents the number of elements greater than a[i] and in left side of it ( from 0 to i-1 ).
- Find the smaller_right array. smaller_right[i] represents the number of elements smaller than a[i] and in right side to it ( from i+1 to n-1 )
- The final answer will be the sum of the product of greater_left[i] and smaller_right[i] for every index.
Below is the implementation of the above approach:
C++
// CPP program to find triplets // a[i]>a[j]>a[k] and i<j<k #include<bits/stdc++.h> using namespace std; // Updates a node in Binary Index Tree (BIT) // at given index(i) in BIT. The given value // 'val' is added to BITree[i] and // all of its ancestors in tree. void update(int BIT[], int n, int i, int val) { for (; i <= n; i += (i & -i)) { BIT[i] += val; } } // Returns sum of arr[0..i]. This function // assumes that the array is preprocessed // and partial sums of array elements are // stored in BIT[]. int query(int BIT[], int i) { int sum = 0; for (; i > 0; i -= (i & -i)) { sum += BIT[i]; } return sum; } // Converts an array to an array with values from 1 to n // and relative order of smaller and greater elements // remains same. For example, {7, -90, 100, 1} is // converted to {3, 1, 4 ,2 } void Convert(int arr[], int n) { int temp[n]; for (int i = 0; i < n; i++) { temp[i] = arr[i]; } sort(temp, temp + n); for (int i = 0; i < n; i++) { arr[i] = lower_bound(temp, temp + n, arr[i]) - temp + 1; } } // Function to find triplets int getCount(int arr[], int n) { // Decomposition Convert(arr, n); int BIT[n + 1] = { 0 }; int smaller_right[n + 1] = { 0 }; int greater_left[n + 1] = { 0 }; // Find all right side smaller elements for (int i = n - 1; i >= 0; i--) { smaller_right[i] = query(BIT, arr[i]-1); update(BIT, n, arr[i], 1); } for (int i = 0; i <= n; i++) { BIT[i] = 0; } // Find all left side greater elements for (int i = 0; i < n; i++) { greater_left[i] = i - query(BIT, arr[i]); update(BIT, n, arr[i], 1); } // Find the required answer int ans = 0; for (int i = 0; i < n; i++) { ans += greater_left[i] * smaller_right[i]; } // Return the required answer return ans; } // Driver code int main() { int arr[] = { 7, 3, 4, 3, 3, 1}; int n = sizeof(arr) / sizeof(arr[0]); cout << getCount(arr, n) << endl; return 0; } |
chevron_right
filter_none
Python3
# Python3 program to find triplets # a[i]>a[j]>a[k] and i<j<k from bisect import bisect_left as lower_bound # Updates a node in Binary Index Tree (BIT) # at given index(i) in BIT. The given value # 'val' is added to BITree[i] and # all of its ancestors in tree. def update(BIT, n, i, val): while i <= n: BIT[i] += val i += (i & -i) # Returns sum of arr[0..i]. This function # assumes that the array is preprocessed # and partial sums of array elements are # stored in BIT[]. def query(BIT, i): summ = 0 while i > 0: summ += BIT[i] i -= (i & -i) return summ # Converts an array to an array with values # from 1 to n and relative order of smaller # and greater elements remains same. For example, # {7, -90, 100, 1} is converted to {3, 1, 4 ,2 } def convert(arr, n): temp = [0] * n for i in range(n): temp[i] = arr[i] temp.sort() for i in range(n): arr[i] = lower_bound(temp, arr[i]) + 1 # Function to find triplets def getCount(arr, n): # Decomposition convert(arr, n) BIT = [0] * (n + 1) smaller_right = [0] * (n + 1) greater_left = [0] * (n + 1) # Find all right side smaller elements for i in range(n - 1, -1, -1): smaller_right[i] = query(BIT, arr[i] - 1) update(BIT, n, arr[i], 1) for i in range(n + 1): BIT[i] = 0 # Find all left side greater elements for i in range(n): greater_left[i] = i - query(BIT, arr[i]) update(BIT, n, arr[i], 1) # Find the required answer ans = 0 for i in range(n): ans += greater_left[i] * smaller_right[i] # Return the required answer return ans # Driver Code if __name__ == "__main__": arr = [7, 3, 4, 3, 3, 1] n = len(arr) print(getCount(arr, n)) # This code is contributed by # sanjeev2552 |
chevron_right
filter_none
Output:
8
Recommended Posts:
- Find maximum sum of triplets in an array such than i < j < k and a[i] < a[j] < a[k]
- Find triplets in an array whose AND is maximum
- Number of triplets in array having subarray xor equal
- Find all triplets in a sorted array that forms Geometric Progression
- Count number of triplets with product equal to given number with duplicates allowed
- Find all triplets with zero sum
- Count number of triplets with product equal to given number
- Count number of triplets with product equal to given number | Set 2
- Maximum value of XOR among all triplets of an array
- Triplets in array with absolute difference less than k
- Count of triplets in an array that satisfy the given conditions
- Print all triplets in sorted array that form AP
- Number of unique triplets whose XOR is zero
- Given a sorted array and a number x, find the pair in array whose sum is closest to x
- Find a number which give minimum sum when XOR with every number of array of integers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


