You are viewing a single comment's thread. Return to all comments →
Simulation Used
def almostSorted(arr): nums = sorted(arr) if nums == arr: return "yes" swaps = [] for i in range(len(arr)): if arr[i] != nums[i]: swaps.append(i+1) if len(swaps) == 2: print('yes') print('swap', swaps[0], swaps[1]) else: if len(arr) > 2: reverse = [arr[i] for i in range(swaps[0]-1, swaps[-1])] if sorted(reverse) == reverse[::-1]: print('yes') print("reverse", swaps[0], swaps[-1]) else: print("no")
Seems like cookies are disabled on this browser, please enable them to open this website
I agree to HackerRank's Terms of Service and Privacy Policy.
Almost Sorted
You are viewing a single comment's thread. Return to all comments →
Simulation Used