Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Find the Minimum length Unsorted Subarray, sorting which makes the complete array sorted in Python
When working with unsorted arrays, we often need to find the minimum length subarray that, when sorted, makes the entire array sorted. For example, in the array [2,6,4,8,10,9,15], sorting the subarray [6,4,8,10,9] at positions 1-5 would make the entire array sorted.
Algorithm Approach
The solution compares the original array with its sorted version to identify mismatched positions ?
Create a sorted copy of the input array
Compare each element with its sorted counterpart
Track all positions where elements differ
Return the distance between first and last mismatched positions
Implementation
class Solution:
def findUnsortedSubarray(self, nums):
sorted_nums = sorted(nums)
mismatched_positions = []
# Find all positions where elements differ
for i in range(len(nums)):
if nums[i] != sorted_nums[i]:
mismatched_positions.append(i)
# Handle edge cases
if len(mismatched_positions) == 0:
return 0 # Array is already sorted
if len(mismatched_positions) == 1:
return 1 # Only one element out of place
# Return length of subarray from first to last mismatch
return mismatched_positions[-1] - mismatched_positions[0] + 1
# Test the solution
solution = Solution()
print(solution.findUnsortedSubarray([2,6,4,8,10,9,15]))
5
Step-by-Step Example
Let's trace through the example [2,6,4,8,10,9,15] ?
nums = [2,6,4,8,10,9,15]
sorted_nums = [2,4,6,8,9,10,15]
# Compare positions:
# Index 0: nums[0]=2, sorted[0]=2 ? (match)
# Index 1: nums[1]=6, sorted[1]=4 ? (mismatch)
# Index 2: nums[2]=4, sorted[2]=6 ? (mismatch)
# Index 3: nums[3]=8, sorted[3]=8 ? (match)
# Index 4: nums[4]=10, sorted[4]=9 ? (mismatch)
# Index 5: nums[5]=9, sorted[5]=10 ? (mismatch)
# Index 6: nums[6]=15, sorted[6]=15 ? (match)
mismatched_positions = [1, 2, 4, 5]
result = 5 - 1 + 1 = 5
print(f"Original: {nums}")
print(f"Sorted: {sorted_nums}")
print(f"Subarray to sort: {nums[1:6]}")
print(f"Length: {len(nums[1:6])}")
Original: [2, 6, 4, 8, 10, 9, 15] Sorted: [2, 4, 6, 8, 9, 10, 15] Subarray to sort: [6, 4, 8, 10, 9] Length: 5
Time and Space Complexity
| Aspect | Complexity | Explanation |
|---|---|---|
| Time | O(n log n) | Sorting the array dominates |
| Space | O(n) | Creating sorted copy and position list |
Alternative Test Cases
solution = Solution() # Already sorted array print(solution.findUnsortedSubarray([1,2,3,4])) # Reverse sorted array print(solution.findUnsortedSubarray([4,3,2,1])) # Single element out of place print(solution.findUnsortedSubarray([1,3,2,4]))
0 4 2
Conclusion
This approach efficiently finds the minimum unsorted subarray by comparing with a sorted version. The algorithm handles edge cases like already sorted arrays and provides O(n log n) time complexity due to the sorting step.
