FunctionRacerProblemsRace

Insert Into a Descending List

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

nums is sorted from largest to smallest and may contain repeats. Return the index at which value would have to be inserted so that the list stays sorted from largest to smallest. When value is equal to items already in the list, it goes after all of them, at the rightmost valid position. Do not actually insert anything, just return the index.

For an empty list the answer is 0. If value is smaller than every item, the answer is len(nums).

Example 1

nums [9,7,7,4,1]
value 7
returns 3

The two 7s sit at indices 1 and 2, and the new 7 goes after them, at index 3.

Example 2

nums [9,7,7,4,1]
value 10
returns 0

10 is bigger than everything, so it goes at the front.

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