Find a triplet in an array such that arr[i] arr[k] and i < j < k
Last Updated :
28 Feb, 2022
Given an array arr[] consisting of a permutation of first N natural numbers, the task is to find a triplet (i, j, k) from the given array such that arr[i] < arr[j] > arr[k], where (i < j < k). If multiple triplets exist, then print any valid triplet of indices. Otherwise, print -1.
Examples:
Input: arr[] = {2, 1, 4, 3}
Output: 1 2 3
Explanation: Triplet that satisfy the given condition is (arr[1], arr[2], arr[3])
Therefore, the required output is 1 2 3.
Input: arr[] = {1, 2, 3, 4, 5}
Output: -1
Naive Approach: The simplest approach to solve this problem is to traverse the array and generate all possible triplets of the given array and for each triplet, check if it satisfies the given conditions or not. If found to be true, then print that triplet. Otherwise, print -1.
Time Complexity: O(N3)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach the idea is based on the following observations:
- If the given array is sorted in ascending order or descending order from the index range [1, N – 2], then the solution does not exist.
- Otherwise, at least one index exists in the given array such that the element just before and after that concerning index is less than the current element.
Follow the steps below to solve the problem:
- Traverse the given array and for each array index, check if the element just before and after the current index is less than the current element or not. If found to be true, then print that the triplet (i – 1, i, i + 1).
- Otherwise, print -1.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
void FindTrip( int arr[], int N)
{
for ( int i = 1; i < N - 1; i++)
{
int p = arr[i - 1];
int q = arr[i];
int r = arr[i + 1];
if (p < q && q > r)
{
cout << i - 1 << " "
<< i << " " << i + 1;
return ;
}
}
cout << -1;
}
int main()
{
int arr[] = { 2, 1, 4, 3 };
int N = sizeof (arr) / sizeof (arr[0]);
FindTrip(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void FindTrip( int arr[],
int N)
{
for ( int i = 1 ; i < N - 1 ;
i++) {
int p = arr[i - 1 ];
int q = arr[i];
int r = arr[i + 1 ];
if (p < q && q > r) {
System.out.println(
(i - 1 ) + " "
+ (i) + " "
+ (i + 1 ));
return ;
}
}
System.out.println(- 1 );
}
public static void main(String args[])
{
int arr[] = { 2 , 1 , 4 , 3 };
int N = arr.length;
FindTrip(arr, N);
}
}
|
Python3
def FindTrip(arr, N):
for i in range ( 1 , N - 1 ):
p = arr[i - 1 ]
q = arr[i]
r = arr[i + 1 ]
if (p < q and q > r):
print (i - 1 , i, i + 1 )
return
print ( - 1 )
if __name__ = = '__main__' :
arr = [ 2 , 1 , 4 , 3 ]
N = len (arr)
FindTrip(arr, N)
|
C#
using System;
class GFG{
static void FindTrip( int [] arr, int N)
{
for ( int i = 1; i < N - 1; i++)
{
int p = arr[i - 1];
int q = arr[i];
int r = arr[i + 1];
if (p < q && q > r)
{
Console.WriteLine((i - 1) + " " +
(i) + " " + (i + 1));
return ;
}
}
Console.WriteLine(-1);
}
public static void Main()
{
int [] arr = { 2, 1, 4, 3 };
int N = arr.Length;
FindTrip(arr, N);
}
}
|
Javascript
<script>
function FindTrip(arr, N)
{
for (let i = 1; i < N - 1;
i++) {
let p = arr[i - 1];
let q = arr[i];
let r = arr[i + 1];
if (p < q && q > r) {
document.write(
(i - 1) + " "
+ (i) + " "
+ (i + 1));
return ;
}
}
document.write(-1);
}
let arr = [2, 1, 4, 3 ];
let N = arr.length;
FindTrip(arr, N);
</script>
|
Output:
1 2 3
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...