Skip to content

First True With Lower Bound
Step by Step

Lower bound keeps the answer inside [lo, hi] and halves the range. Values that are too small move lo past mid, and big enough values pull hi down to mid.

Step through every halving in both directions, then share a link to the step where the range shrank.

What You Watch While the Range Halves

The range, mid and the greyed out half change on every step.

Lower bound finds the first position where a value is big enough. Halving the range gets there in log n checks.

CodeStory's visualiser shows the search range halving and the discarded half greyed out. Marked code lines follow every move, and you can step back, step forward or play.

Open Duplicates of the target first. Then load Target in the middle to see why hi = mid − 1 fails.

How Lower Bound Finds the First True

Steps below follow the range on your screen.

  1. 1Keep the answer inside a range from lo to hi.
  2. 2Look at the middle of that range.
  3. 3Move lo past mid when the value is too small, or pull hi down to mid when it is big enough.
  4. 4Stop when lo and hi meet. That position is the answer.

Lower Bound Mistakes the Visualiser Shows

A switch in the visualiser turns the mistake on, so you can watch the state go wrong.

  • Bug: hi = mid − 1Throws mid away even when nums[mid] ≥ target, so the answer itself can leave [lo, hi]. Load Target in the middle or Duplicates to see it.

Lower Bound Inputs Worth Trying

Five presets cover duplicates, missing targets and both ends of the array.

  • Duplicates of the targetFinds the first 5, not just any 5. Switch on the hi = mid − 1 bug here.
  • Target in the middleThe very first mid is the answer. Switch on the hi = mid − 1 bug and watch the answer leave.
  • Target not in the arrayAnswer is the spot where 7 would be inserted.
  • Smaller than alllo never moves, so the answer is 0.
  • Larger than allhi never moves, so the answer is n, past the end.

Lower Bound Cost in Time and Space

Halving the range on every check keeps the time at log n.

Time
O(log n)counted from the checks you step through
Space
O(1)two index values and nothing else
Pattern
Binary searchHalve the search range with every check.

Practise Binary Search Beyond Lower Bound

Watching shows the range shrink and solving trains you to define the true condition.

Everything in the App in One Place

Practice, interviews and tools all start free. Swipe to see the rest.

Sign up for 100 AI credits now and get 100 more every month. Start free in the app

Lower Bound Visualiser FAQs

Open the visualiser and follow the range as it halves.

Open the visualiser
What does the lower bound visualiser show?

CodeStory's visualiser shows the search range halving and the discarded half greyed out, beside the highlighted code. Move back and forth through every step, or let it play.

What is the time complexity of lower bound?

O(log n) time and O(1) space. Live counts show the range halving instead of a formula on a slide.

Why does my lower bound code fail on some inputs?

Setting hi to mid − 1 is a common cause. Switch the bug on in the visualiser and watch the answer leave the range.

Can I use my own input?

Yes. Enter your own input and the visualiser plays the algorithm on it, including the edge cases that break a solution in an interview.

Is the visualiser free?

Yes. Every new account starts with 100 AI credits and gets 100 more monthly, while watching an algorithm costs nothing.

Stuck on Binary Search? Get a Free Callback

Tell CodeStory which binary search step loses you, and a mentor plans what comes next.

  1. 1CodeStory checks your level, from binary search basics to interview ready.
  2. 2CodeStory explains online and campus batches, with timings for your weekdays or weekends.
  3. 3You decide. No pressure, no payment on the call.

Get a free callback

I am a
I want to learn

Your number is only used for a CodeStory callback.