Back to Arrays
Q25Question 25 of 109

Find Minimum in Rotated Sorted Array: Binary Search on Inflection

Key Concepts
  • A rotated sorted array has two sorted runs with the minimum at the start of the second run (the pivot).
  • Binary search locates the pivot in O(log n) time by comparing the middle element to the right boundary.
  • If middle is greater than right, the minimum lies strictly to the right of middle, so set left to mid plus one.
  • Otherwise the minimum is at middle or to its left, so set right to mid.
  • Comparing to the right boundary rather than the left cleanly handles every rotation including zero rotation.
  • For duplicates, when middle equals right, shrink right by one, which degrades worst case to O(n) but stays correct.
  • Use BigDecimal compareTo for exact comparison of financial rate values.

Definition

Compare middle to the right boundary and keep the half that still contains the drop point.
  • Rotated sorted array – a sorted array shifted so it wraps around at one pivot point, creating two ascending runs.
  • Minimum location – the minimum sits at the pivot, the single place where a larger element is immediately followed by a smaller one.
  • Binary search on inflection – compare a[mid] to a[right]; if a[mid] > a[right], the pivot is to the right, otherwise at mid or to the left.
  • O(log n) time, O(1) space – each iteration discards half the array.
  • Duplicate handling – when a[mid] == a[right], shrink right by one to resolve ambiguity.

The National Bank of India has a list of interest-rate checkpoints that were sorted ascending but then rotated because the cycle restarted partway through the year. An analyst needs the lowest rate, which sits exactly at the point where the sequence drops from a high back to its restart low. Instead of scanning all checkpoints, the analyst repeatedly looks at the middle checkpoint and compares it to the last one, throwing away half the list each time.

Understanding the Concepts

What is a rotated sorted array?

A sorted array rotated at some pivot has two sorted runs: a higher run at the front and a lower run at the back. The overall minimum sits at the very start of the back run, which is the pivot. For example, [4, 5, 6, 7, 0, 1, 2] is a sorted array [0, 1, 2, 4, 5, 6, 7] rotated by 4 positions, and the minimum is 0 at index 4.

Why use binary search instead of linear scan?

The naive approach scans for the one place where an element is greater than its next neighbor, which is O(n). Binary search finds the pivot in O(log n) time by always keeping the half that must contain it. For large datasets of rate checkpoints at the National Bank of India, this logarithmic search is essential.

How does the binary search work internally?

You keep a left and right index over the whole array. At each step you compute the mid index and compare a[mid] to a[right].

DIAGRAM
- If `a[mid] > a[right]`, the pivot must be strictly to the right of mid, because a sorted run would never have its middle larger than its end. Set `left = mid + 1`.
- Otherwise, `a[mid] <= a[right]`, meaning the run from mid to right is properly sorted and the minimum is at mid or to its left. Set `right = mid`.

You stop when left == right, and that index holds the minimum.

DIAGRAM
Array: [4, 5, 6, 7, 0, 1, 2]

Step 1: left=0, right=6, mid=3 → a[3]=7 > a[6]=2 → left=4
Step 2: left=4, right=6, mid=5 → a[5]=1 ≤ a[6]=2 → right=5
Step 3: left=4, right=5, mid=4 → a[4]=0 ≤ a[5]=1 → right=4
Step 4: left=4 == right=4 → answer is a[4] = 0

How do you implement it? (with code)

JAVA
package com.bank.analysis;

import java.math.BigDecimal;

public class MinInRotatedArray {

    public static BigDecimal findMin(BigDecimal[] rates) {
        if (rates == null || rates.length == 0) {
            throw new IllegalArgumentException("Array must not be empty");
        }
        int lo = 0, hi = rates.length - 1;

        while (lo < hi) {
            int mid = lo + (hi - lo) / 2;
            if (rates[mid].compareTo(rates[hi]) > 0) {
                lo = mid + 1;  // pivot is to the right
            } else {
                hi = mid;      // pivot is at mid or to the left
            }
        }
        return rates[lo];
    }

    public static void main(String[] args) {
        BigDecimal[] rates = {
            new BigDecimal("7.5"), new BigDecimal("8.0"),
            new BigDecimal("8.5"), new BigDecimal("3.0"),
            new BigDecimal("4.5"), new BigDecimal("6.0")
        };
        System.out.println("Minimum rate: " + findMin(rates));
    }
}
TEXT
Minimum rate: 3.0

Why compare to the right boundary instead of the left?

Comparing to the right boundary rather than the left is deliberate and important. If you compared a[mid] to a[left] instead, a fully non-rotated array or certain rotations would be ambiguous and require extra cases. Comparing to a[right] cleanly decides which half holds the pivot in every rotation, including zero rotation where the array is already sorted and the answer is the first element.

JAVA
// Comparing to right: works for all rotations including no rotation
if (rates[mid].compareTo(rates[hi]) > 0) lo = mid + 1;
else hi = mid;

// Comparing to left: fails for no rotation [1,2,3,4,5]
// a[mid]=3 > a[left]=1 is true, but minimum is at left, not right

How do you handle duplicates?

If the array can contain duplicate values, the comparison of mid to right can tie. When a[mid] == a[right], you cannot tell which half holds the minimum. You safely shrink the right boundary by one. This degrades the worst case to O(n) when the array is all equal values, which is unavoidable, but keeps it O(log n) on average.

JAVA
public static BigDecimal findMinWithDuplicates(BigDecimal[] rates) {
    int lo = 0, hi = rates.length - 1;
    while (lo < hi) {
        int mid = lo + (hi - lo) / 2;
        int cmp = rates[mid].compareTo(rates[hi]);
        if (cmp > 0) {
            lo = mid + 1;
        } else if (cmp < 0) {
            hi = mid;
        } else {
            hi--;  // cannot decide, shrink by one
        }
    }
    return rates[lo];
}

What are the edge cases?

  • No rotation – the array is already sorted; a[mid] is always less than or equal to a[right], so right keeps moving left until left == right == 0, returning the first element.
  • Single element – the loop does not execute; the single element is the minimum.
  • Two elements – one comparison decides which is smaller.
  • All equal elements – with duplicates handling, the right pointer shrinks one step at a time until convergence.

What about time and space complexity?

| Scenario | Time | Space |
|---|---|---|
| Distinct elements | O(log n) | O(1) |
| With duplicates (average) | O(log n) | O(1) |
| With duplicates (worst: all equal) | O(n) | O(1) |
| Linear scan (naive) | O(n) | O(1) |

What should a senior engineer know?

A senior engineer should know that mid = lo + (hi - lo) / 2 avoids integer overflow compared to (lo + hi) / 2. For financial rate values, always compare with BigDecimal.compareTo rather than primitive operators so exact decimal values are ordered correctly. The zero-rotation case is not a special case but falls naturally out of the compare-to-right logic.

What are common interview follow-up questions?

  • What if there are duplicates? – Add the hi-- branch when mid equals right.
  • Can you also find the index of the minimum? – Yes, return lo instead of rates[lo].
  • What about searching for a target in a rotated array? – That is a separate problem (Search in Rotated Sorted Array) that uses the sorted-half detection technique.

Visual Overview

DIAGRAM
┌──────────────────────────────────────────────────┐
│  Rotated Sorted Array Structure                  │
├──────────────────────────────────────────────────┤
│                                                  │
│   Higher run          Lower run                  │
│   ┌─────────┐         ┌─────────┐                │
│   │ 4 5 6 7 │  pivot  │ 0 1 2   │                │
│   └─────────┘    ↓    └─────────┘                │
│            7 ──→ 0   (the drop)                  │
│                  ↑                                │
│              minimum                             │
├──────────────────────────────────────────────────┤
│  Binary Search Decision                          │
│                                                  │
│   a[mid] > a[right]?                             │
│      YES → pivot is to the RIGHT → lo = mid + 1 │
│      NO  → pivot is at mid or LEFT → hi = mid   │
│                                                  │
│   Stop when lo == hi → a[lo] is the minimum      │
└──────────────────────────────────────────────────┘

Key Takeaway

Find Minimum in Rotated Sorted Array locates the pivot of a sorted array that has been rotated, the single point where a larger element is immediately followed by a smaller one, which is where the minimum sits. You solve it with binary search in O(log n) time and O(1) space by comparing the middle element to the right boundary: if the middle is greater than the right, the pivot must be strictly to the right of middle, otherwise it is at middle or to its left.

Comparing to the right boundary rather than the left is the deliberate choice that makes this clean. Comparing to the left leaves fully sorted or certain rotations ambiguous and forces extra cases, whereas comparing to the right cleanly decides which half holds the pivot in every rotation, including the zero-rotation case where the array is already sorted and the answer is simply the first element.

The senior nuance is duplicates. When values can repeat, the middle can tie with the right boundary and you cannot tell which half holds the minimum, so you safely shrink the right boundary by one, which degrades the worst case to O(n) for an all-equal array but keeps it O(log n) otherwise. For financial rate values, compare with BigDecimal compareTo so exact decimal rates are ordered correctly.

In a banking project at the National Bank of India, this is used on interest-rate checkpoints that were recorded ascending but rotated when the rate cycle restarted mid-year, letting the treasury feature find the lowest rate instantly even over a long list.

Remember one sentence: Compare mid to the right boundary, keep the half that contains the drop, and converge to the minimum in O(log n) time.

---

Share
Use ← → arrow keys to navigate