VOOZH about

URL: https://www.geeksforgeeks.org/dsa/minimum-length-unsorted-subarray-sorting-which-makes-the-complete-array-sorted/

⇱ Smallest Subarray to be Sorted to make the whole array sorted - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Smallest Subarray to be Sorted to make the whole array sorted

Last Updated : 23 Jul, 2025

Given an unsorted array arr[]. Find the subarray arr[s...e] such that sorting this subarray makes the whole array sorted.

Note: If the given array is already sorted then return [0, 0].

Examples:

Input: arr[] = [10, 12, 20, 30, 25, 40, 32, 31, 35, 50, 60]
Output: [3, 8]
Explanation: Sorting subarray starting from index 3 and ending at index 8 results in sorted array. Initial array: [10, 12, 20, 30, 25, 40, 32, 31, 35, 50, 60], Final array: [10, 12, 20, 25, 30, 31, 32, 35, 40, 50, 60](After sorting the bold part).

Input: arr[] = [0, 1, 15, 25, 6, 7, 30, 40, 50]
Output: [2, 5]
Explanation: Sorting subarray starting from index 2 and ending at index 5 results in sorted array. Initial array: [0, 1, 15, 25, 6, 7, 30, 40, 50], Final array: [0, 1, 6, 7, 15, 25, 30, 40, 50](After sorting the bold part).

Input: arr[] = [30, 20, 10]
Output: [0, 2]
Explanation: We need to sort the whole array to make it sorted

[Naive Approach] - Using Sorting - O(n * log n) Time and O(n) Space

The idea is create an auxiliary array temp[] equal to given array arr[], and sort the temp[] array. Now check from starting at which index the element of the given array and temporary array are unequal and store it in temporary variable s . Repeat the above From the end and store the index at another temporary variable e . The length e-s+1 is the length of smallest unequal subarray, and the indices are [s, e].


Output
2 5

[Better Approach] - Using Stack - O(n) Time and O(n) Space

The idea is to check each element’s nearest smaller element to its left and keep track of the maximum element seen so far. This problem mainly becomes a variation of previous smaller element.

  • For each index i, find the index j of its nearest smaller element to the left using stack.
  • If i - j > 1, update the left index to the minimum j encountered and the right index to i, because an element between j and i is out of order.
  • If i - j ≤ 1, check if the maximum element before i is greater than arr[i]; if so, update the right index appropriately.
  • The subarray to be sorted is defined from the left index + 1 to the right index, and its length is right - left.

Output
2 5

[Expected Approach] - O(n) Time and O(1) Space

The idea is to find the leftmost and rightmost index that are not following the sorted order, and then find the minimum and maximum element in subarray [left, right]. Thereafter, find the first element in [0, left - 1], which is greater than minimum, and the last element in [end + 1, n], which is smaller than maximum. These two indices are the required answer.

Follow the below given steps:

  1. Find the candidate unsorted subarray 
    • Scan from left to right and find the first element which is greater than the next element.
    • Scan from right to left and find the first element which is smaller than the next element.
  2. Check whether sorting the candidate unsorted subarray makes the complete array sorted or not. If not, then include more elements in the subarray. 
    • Find the minimum and maximum values in arr[s..e]. Let minimum and maximum values be min and max.
    • Find the first element (if there is any) in arr[0..s-1] which is greater than min, change s to index of this element.
    • Find the last element (if there is any) in arr[e+1..n-1] which is smaller than max, change e to index of this element.
  3. Print s and e.

Below is the implementation of the above approach:


Output
2 5
Comment
Article Tags:
Article Tags: