Sort Array by Reversing a Subarray

You are given an array of 'N' distinct integers, 'ARR'. Determine if it is possible to sort this array by selecting a continuous subarray and reversing it. Return 'true' if sorting is possible by this method, otherwise return 'false'.

Example:

Input:
ARR = [3, 2, 1]
Output:
true
Explanation:

The entire array can be reversed to get [1, 2, 3], which is sorted.

Input:

ARR = [1, 3, 2, 4]
Output:
true
Explanation:

Reversing the subarray [3, 2] results in the sorted array [1, 2, 3, 4].

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

Help your peers!
Add answer anonymously...
Unthinkable Solutions Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
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

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter