We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Algorithms
- Implementation
- Almost Sorted
- Discussions
Almost Sorted
Almost Sorted
Sort by
recency
|
507 Discussions
|
Please Login in order to post a comment
Simulation Used
def almostSorted(arr): sortArr = sorted(arr) swaps = [] for i in range(len(arr)): if arr[i] != sortArr[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') return
you are probably missing this case where swap can occur at two different index check swap at 2 condition adjacent values and values that are far apart. cam free
https://github.com/jiaqing123/HackerRankApp/blob/master/HackerRankApp/Problems/AlmostSorted.cs