Hackerrank - Almost Sorted Solution
2 min read

Hackerrank - Almost Sorted Solution

Hackerrank - Almost Sorted Solution

Given an array of integers, determine whether the array can be sorted in ascending order using only one of the following operations one time.

  1. Swap two elements.
  2. Reverse one sub-segment.

Determine whether one, both or neither of the operations will complete the task. If both work, choose swap. For instance, given an array  either swap the  and , or reverse them to sort the array. Choose swap. The Output Format section below details requirements.

Function Description

Complete the almostSorted function in the editor below. It should print the results and return nothing.

almostSorted has the following parameter(s):

  • arr: an array of integers

Input Format

The first line contains a single integer , the size of .
The next line contains  space-separated integers  where .


All  are distinct.

Output Format

  1. If the array is already sorted, output yes on the first line. You do not need to output anything else.

If you can sort this array using one single operation (from the two permitted operations) then output yes on the first line and then:

a. If elements can be swapped,  and , output swap l r in the second line.  and  are the indices of the elements to be swapped, assuming that the array is indexed from  to .

b. Otherwise, when reversing the segment , output reverse l r in the second line.  and  are the indices of the first and last elements of the subsequence to be reversed, assuming that the array is indexed from  to .

represents the sub-sequence of the array, beginning at index  and ending at index , both inclusive.

If an array can be sorted by either swapping or reversing, choose swap.

  1. If you cannot sort the array either way, output no on the first line.

Sample Input 1

4 2  

Sample Output 1

swap 1 2

Explanation 1

You can either swap(1, 2) or reverse(1, 2). You prefer swap.

Sample Input 2

3 1 2

Sample Output 2


Explanation 2

It is impossible to sort by one single operation.

Sample Input 3

1 5 4 3 2 6

Sample Output 3

reverse 2 5

Explanation 3

You can reverse the sub-array d[2...5] = "5 4 3 2", then the array becomes sorted.

Solution in Python

def almostSorted(a):
    x = 0
    y = len(a)-1
    while a[x]<=a[x+1]:
    while a[y]>=a[y-1]:

    b = a[x:y+1]

    cond_l = True if x-1<0 else b[0]>=a[x-1]
    cond_r = True if y+1==len(a) else a[y+1]>=b[-1]
    if cond_l and cond_r:
        for i in range(len(b)-1):
            if b[i]>b[i+1]:
            return "yes\nswap %s %s"%(x+1,y+1)
    cond_l = True if x-1<0 else a[x-1]<=b[-1]
    cond_r = True if y+1==len(a) else  b[0]<=a[y+1]
    if cond_l and cond_r:
        for i in range(len(b)-1):
            if b[i]<b[i+1]:
            return "yes\nreverse %s %s"%(x+1,y+1)
    return "no"

n = int(input())
arr = list(map(int, input().split()))

Enjoying these posts? Subscribe for more

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

That's okay. But without advertising-income, we can't keep making this site awesome.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add thepoorcoder.com to your ad blocking whitelist or disable your adblocking software.