All lessons

6. Searching

Binary search variants

0 of 5 activities0%

Reading 1

First true / last false

Open

Lower bound: first index with a[i] >= t. Upper bound: first index with a[i] > t.

Search on answer: binary search the answer space when mid can be checked with a predicate (e.g., minimum capacity).

Predicate must be monotonic.

// lower_bound style
while(lo<hi){
  int mid=lo+(hi-lo)/2;
  if(a[mid]>=t) hi=mid;
  else lo=mid+1;
}

Check 2

Monotonic

Open

Search-on-answer needs the feasibility check to be

Fill in 3

First ge

Open

The first position with value >= t is called the

Try it 4

Lower bound

Open

First >= 4.

main.cpp
Loading editor…
Output will appear here.

Assignment 5

Count < t

Open

Read n, sorted ascending array, t. Print how many elements are strictly < t.

main.cpp
Loading editor…