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.
Here's a "simple" linear solution in python runs in O(n) time complexity.
I handled some special edge cases separately when length of array is two and three, other than that I ran a loop and make note of changes in array
changes to note
1. Number of elements need to be swapped
2. Number of elements in continous reverse order
3. Add all indexes of above elements in a new array
After that just needed to analyze the values of above changes and made some logical checks
Almost Sorted
You are viewing a single comment's thread. Return to all comments →
Here's a "simple" linear solution in python runs in O(n) time complexity. I handled some special edge cases separately when length of array is two and three, other than that I ran a loop and make note of changes in array changes to note 1. Number of elements need to be swapped 2. Number of elements in continous reverse order 3. Add all indexes of above elements in a new array
After that just needed to analyze the values of above changes and made some logical checks