Count of index triplets (i, j, k) in given Array such that i<j<k and a[j]<a[k]<a[i]
Last Updated :
19 Dec, 2021
Given an array arr[], the task is to count the number of triplets such that i < j <k and a[ j ]< a[ k ]< a[ i ].
Examples:
Input: arr[] = {1, 2, 3, 4, 5}
Output: -1
Explanation: There is no triplets of required type.
Input: arr[] = {4, 1, 3, 5}
Output: 1
Explanation: There is a triplet in array a[]: {4, 1, 3 }.
Input: arr[] = {2, 1, -3, -2, 5}
Output: 2
Explanation: There are two triplets of required type: {2, 1, -3}, {1, -3, -2}
Naive Approach: Use three loops to check for all the possible triplets in a[] and count the number of triplets in arr[] such that i<j<k and a[ j ]< a[ k ]< a[ i ]. Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int findTriplets( int a[], int n)
{
int cnt = 0;
for ( int i = 0; i < n; i++)
{
for ( int j = i + 1; j < n; j++)
{
for ( int k = j + 1; k < n; k++)
{
if (a[j] < a[k] && a[k] < a[i])
cnt++;
}
}
}
return cnt;
}
int main()
{
int a[] = {2, 1, -3, -2, 5};
int n = sizeof (a) / sizeof (a[0]);
cout << (findTriplets(a, n));
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int findTriplets( int [] a)
{
int cnt = 0 ;
for ( int i = 0 ; i < a.length; i++) {
for ( int j = i + 1 ; j < a.length; j++) {
for ( int k = j + 1 ; k < a.length; k++) {
if (a[j] < a[k] && a[k] < a[i])
cnt++;
}
}
}
return cnt;
}
public static void main(String[] args)
{
int a[] = { 2 , 1 , - 3 , - 2 , 5 };
System.out.println(findTriplets(a));
}
}
|
Python3
def findTriplets(a):
cnt = 0 ;
for i in range ( len (a)):
for j in range (i + 1 , len (a)):
for k in range (j + 1 , len (a)):
if (a[j] < a[k] and a[k] < a[i]):
cnt + = 1
return cnt;
a = [ 2 , 1 , - 3 , - 2 , 5 ];
print (findTriplets(a));
|
C#
using System;
public class GFG {
public static int findTriplets( int [] a)
{
int cnt = 0;
for ( int i = 0; i < a.Length; i++) {
for ( int j = i + 1; j < a.Length; j++) {
for ( int k = j + 1; k < a.Length; k++) {
if (a[j] < a[k] && a[k] < a[i])
cnt++;
}
}
}
return cnt;
}
public static void Main(String[] args)
{
int []a = { 2, 1, -3, -2, 5 };
Console.WriteLine(findTriplets(a));
}
}
|
Javascript
<script>
function findTriplets(a) {
let cnt = 0;
for (let i = 0; i < a.length; i++) {
for (let j = i + 1; j < a.length; j++) {
for (let k = j + 1; k < a.length; k++) {
if (a[j] < a[k] && a[k] < a[i])
cnt++;
}
}
}
return cnt;
}
let a = [2, 1, -3, -2, 5];
document.write(findTriplets(a));
</script>
|
Time Complexity: O(N3)
Auxiliary Space: O(1)
Efficient Approach: A Stack can be used to optimize the above solution. The stack will keep track of the next smaller and second smaller element on the right side.
Follow the steps below to solve the given problem.
- Create a stack and variable say secondMin = INT_MAX.
- Declare a variable say cnt = 0, to store the number of desired triplets.
- Start traversing from the end of the array a[].
- Check if the current element is greater than the secondMin, if it is that means, the required triplet is found because the stack is keeping track of the next smaller and second smaller elements and the current element satisfies the type. Then, set cnt = cnt + 1.
- If the above condition is not satisfied, update the minimum value in the stack, keep popping until stack is not empty or the current element is not smaller than the top of the stack.
- Push current element in the stack
- At last return cnt as the number of triplets found.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findTriplets(vector< int >& a)
{
int cnt = 0;
stack< int > st;
int secondMin = INT_MAX;
for ( int i = a.size() - 1; i >= 0; i--) {
if (a[i] > secondMin)
cnt++;
while (!st.empty() && st.top() > a[i]) {
secondMin = st.top();
st.pop();
}
st.push(a[i]);
}
return cnt;
}
int main()
{
vector< int > a = { 2, 1, -3, -2, 5 };
cout << findTriplets(a);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static int findTriplets( int [] a)
{
int cnt = 0 ;
Stack<Integer> stack = new Stack<>();
int secondMin = Integer.MAX_VALUE;
for ( int i = a.length - 1 ; i >= 0 ; i--) {
if (a[i] > secondMin)
cnt++;
while (!stack.isEmpty()
&& stack.peek() > a[i]) {
secondMin = stack.pop();
}
stack.push(a[i]);
}
return cnt;
}
public static void main(String[] args)
{
int a[] = { 2 , 1 , - 3 , - 2 , 5 };
System.out.println(findTriplets(a));
}
}
|
Python3
import sys
def findTriplets(a):
cnt = 0
st = []
secondMin = sys.maxsize
for i in range ( len (a) - 1 , - 1 , - 1 ):
if (a[i] > secondMin):
cnt + = 1
while ( not len (st) = = 0 and st[ - 1 ] > a[i]):
secondMin = st[ - 1 ]
st.pop()
st.append(a[i])
return cnt
if __name__ = = "__main__" :
a = [ 2 , 1 , - 3 , - 2 , 5 ]
print (findTriplets(a))
|
C#
using System;
using System.Collections.Generic;
public class GFG {
public static int findTriplets( int [] a)
{
int cnt = 0;
Stack< int > stack = new Stack< int >();
int secondMin = int .MaxValue;
for ( int i = a.Length - 1; i >= 0; i--) {
if (a[i] > secondMin)
cnt++;
while (stack.Count!=0
&& stack.Peek() > a[i]) {
secondMin = stack.Pop();
}
stack.Push(a[i]);
}
return cnt;
}
public static void Main(String[] args)
{
int []a = { 2, 1, -3, -2, 5 };
Console.WriteLine(findTriplets(a));
}
}
|
Javascript
<script>
function findTriplets(a)
{
var cnt = 0;
var stack = [];
var secondMin = Number.MAX_VALUE;
for ( var i = a.length - 1; i >= 0; i--) {
if (a[i] > secondMin)
cnt++;
while (stack.length!=0
&& stack[0] > a[i]) {
secondMin = stack.pop();
}
stack.push(a[i]);
}
return cnt;
}
var a = [ 2, 1, -3, -2, 5 ];
document.write(findTriplets(a));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...