Check whether there exists a triplet (i, j, k) such that arr[i] < arr[k] < arr[j] for i < j < k
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]
Naive Approach: The idea is to generate all possible triplets and if any triplets satisfy the given conditions the print Yes else print No.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool findTriplet(vector< int > nums)
{
for ( int i = 0; i < nums.size(); i++) {
for ( int j = i + 1; j < nums.size(); j++) {
for ( int k = j + 1; k < nums.size(); k++) {
if (nums[i] < nums[k] && nums[k] < nums[j])
return true ;
}
}
}
return false ;
}
int main()
{
vector< int > arr = { 4, 7, 5, 6 };
if (findTriplet(arr)) {
cout << " Yes" << '\n' ;
}
else {
cout << " No" << '\n' ;
}
return 0;
}
|
Python3
def findTriplet(nums):
for i in range ( len (nums)):
for j in range (i + 1 , len (nums)):
for k in range (j + 1 , len (nums)):
if (nums[i] < nums[k] and nums[k] < nums[j]):
return True
return False
arr = [ 4 , 7 , 5 , 6 ]
if (findTriplet(arr)):
print ( " Yes" )
else :
print ( " No" )
|
Javascript
<script>
function findTriplet(nums)
{
for (let i = 0; i < nums.length; i++)
{
for (let j = i + 1; j < nums.length; j++)
{
for (let k = j + 1; k < nums.length; k++)
{
if (nums[i] < nums[k] && nums[k] < nums[j])
return true ;
}
}
}
return false ;
}
let arr = [ 4, 7, 5, 6 ];
if (findTriplet(arr)) {
document.write( " Yes" , "</br>" )
}
else {
document.write( " No" , "</br>" )
}
</script>
|
C#
using System;
public class HelloWorld
{
public static bool findTriplet( int [] nums)
{
for ( int i = 0; i < nums.Length; i++) {
for ( int j = i + 1; j < nums.Length; j++) {
for ( int k = j + 1; k < nums.Length; k++) {
if (nums[i] < nums[k] && nums[k] < nums[j])
return true ;
}
}
}
return false ;
}
public static void Main( string [] args)
{
int []arr = { 4, 7, 5, 6 };
if (findTriplet(arr)) {
Console.WriteLine( "Yes" );
}
else {
Console.WriteLine( "No" );
}
}
}
|
Java
import java.util.*;
class GFG{
public static boolean findTriplet( int [] nums)
{
for ( int i = 0 ; i < nums.length; i++) {
for ( int j = i + 1 ; j < nums.length; j++) {
for ( int k = j + 1 ; k < nums.length; k++) {
if (nums[i] < nums[k] && nums[k] < nums[j])
return true ;
}
}
}
return false ;
}
public static void main(String[] args)
{
int arr[] = { 4 , 7 , 5 , 6 };
if (findTriplet(arr))
{
System.out.println( "Yes" );
}
else
{
System.out.println( "No" );
}
}
}
|
Output:
Yes
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++
#include <bits/stdc++.h>
using namespace std;
bool findTriplet(vector< int >& arr)
{
int n = arr.size();
stack< int > st;
int h3 = INT_MIN, h1 = INT_MAX;
for ( int i = n - 1; i >= 0; i--) {
h1 = arr[i];
while (!st.empty()
&& st.top() < arr[i]) {
h3 = st.top();
st.pop();
}
st.push(arr[i]);
if (h1 < h3) {
return true ;
}
}
return false ;
}
int main()
{
vector< int > arr = { 4, 7, 5, 6 };
if (findTriplet(arr)) {
cout << " Yes" << '\n' ;
}
else {
cout << " No" << '\n' ;
}
return 0;
}
|
Java
import java.util.*;
class GFG{
public static boolean findTriplet( int [] arr)
{
int n = arr.length;
Stack<Integer> st = new Stack<>();
int h3 = Integer.MIN_VALUE;
int h1 = Integer.MAX_VALUE;
for ( int i = n - 1 ; i >= 0 ; i--)
{
h1 = arr[i];
while (!st.empty() && st.peek() < arr[i])
{
h3 = st.peek();
st.pop();
}
st.push(arr[i]);
if (h1 < h3)
{
return true ;
}
}
return false ;
}
public static void main(String[] args)
{
int arr[] = { 4 , 7 , 5 , 6 };
if (findTriplet(arr))
{
System.out.println( "Yes" );
}
else
{
System.out.println( "No" );
}
}
}
|
Python3
import sys
def findTriplet(arr):
n = len (arr)
st = []
h3 = - sys.maxsize - 1
h1 = sys.maxsize
for i in range (n - 1 , - 1 , - 1 ):
h1 = arr[i]
while ( len (st) > 0 and st[ - 1 ] < arr[i]):
h3 = st[ - 1 ]
del st[ - 1 ]
st.append(arr[i])
if (h1 < h3):
return True
return False
if __name__ = = '__main__' :
arr = [ 4 , 7 , 5 , 6 ]
if (findTriplet(arr)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Collections.Generic;
class GFG{
public static bool findTriplet( int [] arr)
{
int n = arr.Length;
Stack< int > st = new Stack< int >();
int h3 = int .MinValue;
int h1 = int .MaxValue;
for ( int i = n - 1; i >= 0; i--)
{
h1 = arr[i];
while (st.Count != 0 && st.Peek() < arr[i])
{
h3 = st.Peek();
st.Pop();
}
st.Push(arr[i]);
if (h1 < h3)
{
return true ;
}
}
return false ;
}
public static void Main(String[] args)
{
int []arr = { 4, 7, 5, 6 };
if (findTriplet(arr))
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
}
|
Javascript
<script>
function findTriplet(arr)
{
var n = arr.length;
var st = [];
var h3 = -1000000000, h1 = 1000000000;
for ( var i = n - 1; i >= 0; i--) {
h1 = arr[i];
while (st.length!=0
&& st[st.length-1] < arr[i]) {
h3 = st[st.length-1];
st.pop();
}
st.push(arr[i]);
if (h1 < h3) {
return true ;
}
}
return false ;
}
var arr = [4, 7, 5, 6 ];
if (findTriplet(arr)) {
document.write( " Yes" );
}
else {
document.write( " No" );
}
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
10 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...