Sort Array
You are given ‘N’ distinct integers in the form of an array ‘ARR’. You need to find whether it is possible to sort the given array by choosing a continuous subarray and reversing that subarray. You have to return “true” if it is possible to sort the array after reversing the subarray. Otherwise, return “false”.
For example:
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].
Input Format :
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.
Output Format :
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.
Note :
You do not need to print anything. It has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 10
1 <= N <= 10^6
1 <= ARR[i] <= 10^9
Time Limit: 1 sec
AnswerBot
1y
The question asks whether it is possible to sort an array by choosing a continuous subarray and reversing it.
Check if the array is already sorted. If yes, return true.
Find the first and last elements ...read more
CodingNinjas
author
2y
Use of sorting algorithms for this question
CodingNinjas
author
2y
Brute Force
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 ba...read more
CodingNinjas
author
2y
Greedy Approach
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 sor...read more
Add answer anonymously...
Top Unthinkable Solutions Software Developer interview questions & answers
Popular interview questions of Software Developer
Top HR questions asked in Unthinkable Solutions Software Developer
>
Unthinkable Solutions Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app