FunctionRacerProblemsRace

Count Between in a Sorted List

EasySorting and searchingPython, JavaScript, TypeScript, Java, C++

nums is sorted in non-decreasing order and may contain repeats. Return how many of its items x satisfy lo <= x <= hi.

Because the list is sorted, two binary searches (the first position holding a value of at least lo, and the first position holding a value above hi) answer this without visiting every item, but any correct method is accepted. An empty list gives 0.

Example 1

nums [1,2,2,3,5,8]
lo 2
hi 5
returns 4

2, 2, 3 and 5 lie between 2 and 5.

Example 2

nums [1,2,2,3,5,8]
lo 6
hi 7
returns 0

Nothing lies between 6 and 7.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Sorting and searching problems