Given an array arr[], the task is to check that if there exist a triplet (i, j, k) such that arr[i]<arr[k]<arr[j] and i<j<k then print Yes else print No.

Examples:

Input:_ arr[] = {1, 2, 3, 4, 5}_

Output:_ No_

Explanation:

There is no such sub-sequence such that arr[i] < arr[k] < arr[j]

Input:_ arr[] = {3, 1, 5, 0, 4}_

Output:_ Yes_

Explanation:

There exist a triplet (3, 5, 4) which is arr[i] < arr[k] < arr[j]

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Naive Approach: The idea is to generate all possible triplets and if any triplets satisfy the given conditions the print Yes else print No.

Time Complexity:_ O(N3) _

Auxiliary Space:_ O(1) _

Efficient Approach: To optimize the above approach the idea is to use the stack to keep the track of the smaller elements in the right of every element in the array arr[]. Below are the steps:

  • Traverse the array from the end and maintain a stack which stores the element in the decreasing order.
  • To maintain the stack in decreasing order, pop the elements which are smaller than the current element. Then mark the popped element as the third element of the triplet.
  • While traversing the array in reverse if any element is less than the last popped element(which is marked as the third element of the triplet). Then their exist a triplet which satisfy the given condition and print Yes.
  • Otherwise print No.

Below is the implementation of the above approach:

  • C++

// C++ program for the above approach

#include <bits/stdc++.h>

**using** **namespace** std;

// Function to check if there exist

// triplet in the array such that

// i < j < k and arr[i] < arr[k] < arr[j]

**bool** findTriplet(vector<``**int**``>& arr)

{

**int** n = arr.size();

stack<``**int**``> st;

// Initialize the heights of h1 and h2

// to INT_MAX and INT_MIN respectively

**int** h3 = INT_MIN, h1 = INT_MAX;

**for** (``**int** i = n - 1; i >= 0; i--) {

// Store the current element as h1

h1 = arr[i];

// If the element at top of stack

// is less than the current element

// then pop the stack top

// and keep updating the value of h3

**while** (!st.empty()

&& st.top() < arr[i]) {

h3 = st.top();

st.pop();

}

// Push the current element

// on the stack

st.push(arr[i]);

// If current element is less

// than h3, then we found such

// triplet and return true

**if** (h1 < h3) {

**return** **true**``;

}

}

// No triplet found, hence return false

**return** **false**``;

}

// Driver Code

**int** main()

{

// Given array

vector<``**int**``> arr = { 4, 7, 5, 6 };

// Function Call

**if** (findTriplet(arr)) {

cout << " Yes" << '\n'``;

}

**else** {

cout << " No" << '\n'``;

}

**return** 0;

}

Output:

Yes

Time Complexity: O(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.

#arrays #greedy #stack #cpp-stack #java #python

Check whether there exists a triplet such that arr[i] < arr[k] < arr[j] for i < j < k
7.60 GEEK