• + 0 comments

    Simple Python solution:

    def almostSorted(arr):
            # Write your code here
    
            sorted_arr = sorted(arr)
    
            if arr == sorted_arr:
                    print("yes")
                    return
    
            wrong = []
            for i, (num, sorted_num) in enumerate(zip(arr, sorted_arr)):
                    if num == sorted_num:
                            continue
                    wrong.append(i+1)
    
            if len(wrong) == 2:
                    print("yes")
                    print(f"swap {wrong[0]} {wrong[1]}")
                    return
    
            if arr[wrong[0]-1:wrong[-1]] == sorted_arr[wrong[0]-1:wrong[-1]][::-1]:
                    print("yes")
                    print(f"reverse {wrong[0]} {wrong[-1]}")
                    return
    
            print("no")
            return