InterviewBit Academy is now Scaler!
InterviewBit Academy is now Scaler Academy!

Reverse Subarray for Sorting

Problem Description

Given an array A containing N distinct integers.

The task is to check whether reversing one sub-array make the array sorted or not. If the array is already sorted or by reversing a subarray once make it sorted return 1, else return 0.

Problem Constraints

1 <= N <= 105

1 <= A[i] <= 106

Input Format

First argument is an integer array A.

Output Format

Return 1 if it is possible to sort the subarray by reversing any subarray else return 0.

Example Input

Input 1:

 A = [1, 2, 5, 4, 3]

Input 2:

 A = [1, 2, 4, 5, 3]

Example Output

Output 1:


Output 2:


Example Explanation

Explanation 1:

 By reversing the subarray [5, 4, 3],the array will be sorted.

Explanation 2:

 Not possible

NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.
Start solving Reverse Subarray for Sorting on Interview Code Editor
Sign Up
to access hints and editorial solutions for Reverse Subarray for Sorting


Click here to start solving coding interview questions