I am attempting this common interview problem Shortest Unsorted Continous subarray Problem statement looks like this.
Given an unsorted array
Aof sizeN. Find the subarray0 <= s < r < Nsuch that sorting this subarray makes the whole array sorted.
My Approach:
My idea was based on selection sort. We can traverse over the given array A choosing the elements A[i]. For every such element chosen, I tried to determine its correct position in the sorted array. For this, I compared A[i] with every A[j], such that i < j < N. But as you can see this approach took O(N*N) time complexity, but the judge expected a solution of O(N) time complexity and obviously O(1) extra space. Can anyone help me with an acceptable solution?