
Ninja is thinking of developing a game based on “Teen Patti” where a user will be provided with some number ‘N’ or in technical terms with an array “ARR[i]” and the user has to find out if there exists a triplet of indices (i, j, k) such that ‘i < j < k’ and ‘ARR[i] < ARR[j] <ARR[k]’ holds true. If such a pair of indices exists the user has to return ‘True’ else he has to return ‘False’.
Your task is to help Ninja find out such triplets indices.
Input Format:
The first line contains an integer 'T' which denotes the number of test cases or queries to be run.
The first line of each test case contains a single integer ‘N’ denoting the size of the array.
The next line contains ‘N’ space-separated strings denoting the values of elements of the given array.
Output Format:
For each test case, print a single line containing ‘True’ or ‘False’ as per the given condition.
The output of each test case will be printed in a separate line.
Note:
You do not need to print anything; it has already been taken care of. Just implement the function.
Constraints:
1 <= T <= 5
1 <= N <= 5000
0 <= i < j < k <= N
1 <= ARR[i] <= 10 ^ 9
Where ‘T’ represents the number of test cases and ‘N’ represents the size of array 'i', 'j', 'k' represents the triplets indices, and ‘ARR[i]’ represents the elements of the array.
Time Limit: 1 second .
2
3
2 1 3
5
1 2 0 4 5
False
True
Test Case 1:
We try to find i, j, k such that ‘i < j < k’ and ‘ARR[i] < ARR[j] < ARR[k]’ but we can see that there is no such pair as only one possible pair exists:
‘2 > 1 < 3’ so it fails our required condition hence we returned ‘False’.
Test Case 2:
For this test case size of the array is ‘5’ and elements of the array are [1, 2, 0, 4, 5] so the pair of indices possible that satisfy required conditions are shown below:
‘i=0, j=1, k=2’ but ‘ARR[0] < ARR[1] > ARR[2] as 1 < 2 >0.
‘i=1, j=2, k=3 but ‘ARR[1] > ARR[2] < ARR[3] as 2 > 0 < 4.
‘i=2, j=3, k=4 now ARR[2] < ARR[3] < ARR[4] as 0 < 4 < 5.
Hence we return ‘True’ as such a pair of indices is present in the array.
2
3
1 1 1
5
1 2 3 4 5
False
True
Test Case 1:
We try to find i, j, k such that ‘i < j < k’ and ‘ARR[i] < ARR[j] < ARR[k]’ but we can see that there is no such pair as only one possible pair exists:
‘1 = 1 = 1’ so it failed our required condition hence we returned ‘False’.
Test Case 2:
For this test case size of the array is ‘5’ and elements of the array are [1, 2, 3, 4, 5] so the pair of indices possible that satisfy required conditions are shown below:
‘i=0, j=1, k=2’ ‘ARR[0] < ARR[1] < ARR[2] as 1 < 2 < 3.
‘i=1, j=2, j=3 ‘ARR[1] < ARR[2] < ARR[3] as 2 < 3 < 4.
‘i=2, j=3, k=4 now ARR[2] < ARR[3] < ARR[4] as 3 < 4 < 5.
Hence we return ‘True’ as such a pair of indices is present in the array.
Try to check possible tuples of ‘i, j, k’.
The idea here is to check all possible tuples(i, j, k) that satisfy given conditions. For this, we will use 3 nested loops.
O(N ^ 3), Where ‘N’ is the size of an array.
As each loop takes O(N) time so O(N) * O(N) * O(N) = O(N ^ 3)
O(1).
No extra space is being used.