
Let 'N' = 5, 'A' = [1, 5, 2, 4, 3].
Here, if we take 'i' = 0, 'j' = 1, and 'k' = 3, then 'i' < 'j' < 'k' (0 < 1 < 3) and 'A[i]' < 'A[k]' < 'A[j]' (1 < 4 < 5).
Therefore, the answer is True.
The first line contains an integer 'N', the length of the array 'A'.
The second line contains 'N' integers representing the elements of the array 'A', separated by spaces.
Return True if such indices 'i', 'j', and 'k' exist, otherwise return False.
You don’t need to print anything. Just implement the given function.
3 <= 'N' <= 10^5
-10^9 <= 'A[i]' <= 10^9
Time Limit: 1 sec
Approach:
We can iterate through all possible triplets of indices ('i', 'j', 'k') such that 0 <= 'i' < 'j' < 'k' < 'N'. For each triplet, we check if the condition 'A[i]' < 'A[k]' < 'A[j]' holds. If we find such a triplet, we can immediately return True. If we iterate through all possible triplets and don't find any that satisfy the condition, we return False.
Algorithm:
Approach:
We iterate through the array from right to left. For each element 'A[k]', we want to find if there exists a 'j' to its left ('j' < 'k') such that 'A[j]' > 'A[k]', and an 'i' to the left of 'j' ('i' < 'j') such that 'A[i]' < 'A[k]'. We can maintain a stack to store potential 'A[k]' values. We also precompute the minimum value to the left of each index.
Algorithm: