


Let ‘ARR’ be: [3, 2, 1]
We can pick the whole array as a subarray and reverse it to get the sorted array [1, 2, 3].
The first line of input contains an integer ‘T’, denoting the number of test cases.
The first line of each test case contains a single integer ‘N’, representing the size of the array.
The second line of each test case contains ‘N’ space-separated integers, representing the array ‘ARR’ elements.
For each test case, print ‘true’ if it is possible to sort the array after reversing the subarray, else print ‘false’.
Print output of each test case in a separate line.
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^6
1 <= ARR[i] <= 10^9
Time Limit: 1 sec
The basic idea is to reverse all the possible subarrays and check if the array is sorted or not. If the array is sorted, we simply return true. Else we again reverse that subarray to get back the original array and check for other subarrays possible.
Here is the algorithm:
The basic idea is to find the first subarray that needs to be reversed to get the sorted array. We reverse the chosen subarray and check whether the array is sorted or not. If it is sorted, we print the range, and else the array cannot be sorted.
Here is the algorithm :