Finding Where a New Score Fits
Halcyon Bank keeps a board of branch scores that is held in increasing order at all times. When a branch is rescored, the software works out where the new entry belongs before it inserts it, so that it knows how much of the board has to shift. If the new score matches scores already on the board, the new entry goes in ahead of them — the earliest position that keeps the board in order.
Task: Print the position at which the new score belongs.
Input
Three lines. The first holds n, how many scores are on the board. The second holds those n scores on a single line, separated by single spaces, already in increasing order. The third holds the new score on its own.
Output
One line holding the position, counted from 0. A new score below everything on the board belongs at position 0; a new score above everything belongs at position n, one past the last existing entry.
Example:
Input:
4
10 20 30 40
25
Output:
2
Sign in to solve this problem
Reading problems is free for everyone — solving them (Run, Submit, and tracking what you've solved) needs an account.
Sign in