🧠
Binary Search with Early Exit on Sorted Subarray
💡 This approach adds a check to quickly return if the current search space is already sorted, improving efficiency in some cases.
Intuition
If the subarray between low and high is sorted, the minimum is at low. Otherwise, use the modified binary search logic to narrow down the minimum.
Algorithm
- Initialize low and high pointers.
- While low < high:
- If nums[low] < nums[high], return nums[low] immediately.
- Calculate mid index.
- If nums[mid] > nums[high], move low to mid + 1.
- Else if nums[mid] < nums[high], move high to mid.
- Else decrement high by 1.
- Return nums[low] after loop.
💡 The early exit check avoids unnecessary searching when the subarray is already sorted.
def find_min(nums):
low, high = 0, len(nums) - 1
while low < high:
if nums[low] < nums[high]:
return nums[low]
mid = (low + high) // 2
if nums[mid] > nums[high]:
low = mid + 1
elif nums[mid] < nums[high]:
high = mid
else:
high -= 1
return nums[low]
# Driver code
if __name__ == '__main__':
arr = [2,2,2,0,1]
print(find_min(arr)) # Output: 0
Line Notes
if nums[low] < nums[high]:Check if current subarray is sorted to return early
return nums[low]Low points to minimum after loop ends
mid = (low + high) // 2Calculate mid index for binary search
if nums[mid] > nums[high]:Minimum lies right of mid, move low up
elif nums[mid] < nums[high]:Minimum lies at or left of mid, move high down
else:Duplicates found, shrink high to reduce search space
public class Solution {
public static int findMin(int[] nums) {
int low = 0, high = nums.length - 1;
while (low < high) {
if (nums[low] < nums[high]) {
return nums[low];
}
int mid = low + (high - low) / 2;
if (nums[mid] > nums[high]) {
low = mid + 1;
} else if (nums[mid] < nums[high]) {
high = mid;
} else {
high--;
}
}
return nums[low];
}
public static void main(String[] args) {
int[] arr = {2,2,2,0,1};
System.out.println(findMin(arr)); // Output: 0
}
}
Line Notes
if (nums[low] < nums[high])Check if current subarray is sorted to return early
return nums[low];Low points to minimum after loop
int mid = low + (high - low) / 2;Calculate mid index safely
if (nums[mid] > nums[high])Minimum lies right of mid, move low up
else if (nums[mid] < nums[high])Minimum lies at or left of mid, move high down
elseDuplicates found, shrink high to reduce search space
#include <iostream>
#include <vector>
using namespace std;
int findMin(vector<int>& nums) {
int low = 0, high = nums.size() - 1;
while (low < high) {
if (nums[low] < nums[high]) {
return nums[low];
}
int mid = low + (high - low) / 2;
if (nums[mid] > nums[high]) {
low = mid + 1;
} else if (nums[mid] < nums[high]) {
high = mid;
} else {
high--;
}
}
return nums[low];
}
int main() {
vector<int> arr = {2,2,2,0,1};
cout << findMin(arr) << endl; // Output: 0
return 0;
}
Line Notes
if (nums[low] < nums[high])Check if current subarray is sorted to return early
return nums[low];Low points to minimum after loop
int mid = low + (high - low) / 2;Calculate mid index safely
if (nums[mid] > nums[high])Minimum lies right of mid, move low up
else if (nums[mid] < nums[high])Minimum lies at or left of mid, move high down
elseDuplicates found, shrink high to reduce search space
function findMin(nums) {
let low = 0, high = nums.length - 1;
while (low < high) {
if (nums[low] < nums[high]) {
return nums[low];
}
let mid = Math.floor((low + high) / 2);
if (nums[mid] > nums[high]) {
low = mid + 1;
} else if (nums[mid] < nums[high]) {
high = mid;
} else {
high--;
}
}
return nums[low];
}
// Test
console.log(findMin([2,2,2,0,1])); // Output: 0
Line Notes
if (nums[low] < nums[high])Check if current subarray is sorted to return early
return nums[low];Low points to minimum after loop
let mid = Math.floor((low + high) / 2);Calculate middle index
if (nums[mid] > nums[high])Minimum lies right of mid, move low up
else if (nums[mid] < nums[high])Minimum lies at or left of mid, move high down
elseDuplicates found, shrink high to reduce search space
TimeO(n) worst case due to duplicates, average O(log n)
SpaceO(1)
Early exit can speed up cases where subarrays are sorted, but duplicates can still degrade to linear time.
💡 This optimization helps in many practical cases, reducing unnecessary binary search steps.
Interview Verdict: Accepted and slightly optimized over approach 2
This is a practical enhancement to the modified binary search that can impress interviewers.