![]() |
VOOZH | about |
Given an array arr[] of n integers, find the 3 elements such that a[i] < a[j] < a[k] and i < j < k in O(n) time. If there are multiple such triplets, then print any one of them.
Examples:
Input: arr[] = [12, 11, 10, 5, 6, 2, 30]
Output: 5, 6, 30
Explanation: As 5 < 6 < 30, and they appear in the same sequence in the array
Input: arr[] = [1, 2, 3, 4]
Output: 1, 2, 3
Explanation: As 1 < 2 < 3, and they appear in the same sequence in the array
Input: arr[] = [4, 3, 2, 1]
Output: No such triplet exists.
Table of Content
The main motive is to find an element which has an element smaller than itself on the left side of the array and an element greater than itself on the right side of the array.
5 6 30
First find two elements arr[i] & arr[j] such that arr[i] < arr[j]. Then find a third element arr[k] greater than arr[j]. We can think of the problem in three simple terms.
5 6 30