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
Check if reversing a sub array make the array sorted in Python
Sometimes we need to check if an array can be sorted by reversing exactly one sub-array. This problem involves finding a decreasing sequence and verifying if reversing it would make the entire array sorted.
Problem Understanding
Given an array with unique elements, we need to determine if reversing one sub-array will make the entire array sorted. If the array is already sorted, return True.
For example, in [4,6,27,25,15,9,37,42], reversing the sub-array [27,25,15,9] gives us [4,6,9,15,25,27,37,42], which is sorted.
Algorithm Steps
The solution follows these key steps ?
- Find the first position where the array stops being sorted
- Find the end of the decreasing sequence
- Verify that reversing this sequence maintains sorted order
- Check that the remaining array is already sorted
Implementation
def solve(nums):
n = len(nums)
if n == 1:
return True
# Find first position where array is not sorted
i = 1
for i in range(1, n):
if nums[i - 1] < nums[i]:
if i == n - 1: # Already sorted
return True
else:
break
# Find end of decreasing sequence
j = i
while j < n and nums[j] < nums[j - 1]:
# Check if reversing would break order with previous elements
if i > 1 and nums[j] < nums[i - 2]:
return False
j += 1
# If decreasing sequence goes to end, we can reverse it
if j == n:
return True
# Check if first element of remaining array fits
k = j
if nums[k] < nums[i - 1]:
return False
# Check if rest of array is sorted
while k < n - 1:
if nums[k] >= nums[k + 1]:
return False
k += 1
return True
# Test with example
nums = [4, 6, 27, 25, 15, 9, 37, 42]
result = solve(nums)
print(f"Can array {nums} be sorted by reversing one sub-array? {result}")
# Test with already sorted array
sorted_nums = [1, 2, 3, 4, 5]
result2 = solve(sorted_nums)
print(f"Can array {sorted_nums} be sorted by reversing one sub-array? {result2}")
Can array [4, 6, 27, 25, 15, 9, 37, 42] be sorted by reversing one sub-array? True Can array [1, 2, 3, 4, 5] be sorted by reversing one sub-array? True
How It Works
The algorithm identifies three sections in the array ?
- Ascending section: Elements already in sorted order from the beginning
- Decreasing section: The sub-array that needs to be reversed
- Remaining section: Elements after the decreasing part that should already be sorted
For the solution to work, reversing the decreasing section must create a valid transition between the ascending section and remaining section.
Edge Cases
# Test various edge cases
test_cases = [
[1], # Single element
[1, 2, 3], # Already sorted
[3, 2, 1], # Completely reversed
[1, 3, 2, 4], # Simple case
[4, 3, 2, 1, 5] # Cannot be fixed
]
for nums in test_cases:
result = solve(nums)
print(f"{nums} ? {result}")
[1] ? True [1, 2, 3] ? True [3, 2, 1] ? True [1, 3, 2, 4] ? True [4, 3, 2, 1, 5] ? False
Conclusion
This algorithm efficiently determines if an array can be sorted by reversing one sub-array. It works by identifying the decreasing sequence and validating that its reversal maintains proper ordering with adjacent elements.
