Back to Browse

Minimum length subarray of an unsorted array sorting which results in complete sorted array

7.3K views
Feb 20, 2016
5:05

Given an unsorted array of integers, find the shortest subarray, which upon sorting will result in complete sorted array. Algorithm: 1: From the beginning of the array, move to element in the array up to which the elements are sorted i.e. where array[i] is greater than array[i+1]. Set startIndex = i. 2: From the end of the array, move to the element up to which the elements are sorted in reverse order i.e. where array[j-1] is greater than array[j]. Set endIndex = j. 3: Find the minimum and maximum elements in the subarray from startIndex to endIndex. 4: Search the sorted array from 0 to startIndex to find the index at which minimum element will be in sorted array say, minIndex. 5: Search the sorted array from endIndex to end of array to find the index at which maximum element will be in sorted array say, maxIndex. 6: Resultant sub array is the subarray between minIndex to maxIndex. Order of the Algorithm: Time Complexity: O(n) Space Complexity: O(1) Code and Algorithm Visualization: Coming soon on http://www.ideserve.co.in. Stay Tuned! Website: http://www.ideserve.co.in Facebook: https://www.facebook.com/IDeserve.co.in

Download

0 formats

No download links available.

Minimum length subarray of an unsorted array sorting which results in complete sorted array | NatokHD