DEV Community

Narednra Reddy Yadama
Narednra Reddy Yadama

Posted on

Binary Search Isn’t Just for Sorted Arrays Anymore

We all know classic binary search:
Sorted array → divide → conquer → find the target.
But what if the array isn’t sorted?
Or worse—what if we’re not even searching for an index, but an answer?
That’s where Modified Binary Search comes in 💡

✅ Classic Binary Search
▫️ Works on sorted arrays
▫️ Finds exact index of target
▫️ Time complexity: O(log N)

while (low <= high) {
int mid = low + (high - low) / 2;
if (nums[mid] == target) return mid;
else if (nums[mid] < target) low = mid + 1;
else high = mid - 1;
}

⚡ Modified Binary Search
▫️ No sorted array required
▫️ Search happens in the answer space
▫️ Used in problems like:
🔹 Min/max capacity
🔹Kth smallest/largest
🔹Optimal time, distance, or cost

Think:
🧠 “What’s the smallest value that satisfies the condition?”
That’s binary search on the answer range.

*💡 Spot the Pattern *
If the problem says:
➡️ “Find the minimum…”
➡️ “What’s the max capacity…”
➡️ “Can we do this in X time…”

You’re in modified binary search territory.
Binary search isn’t just a search—it’s a strategy.
Master both flavors, and you’ll unlock a whole new level of algorithmic thinking.

Top comments (0)