Level-Up Rank
Context
Two rival players earn score rewards from separate level runs. Each player's scores are already listed from smallest to largest. The game wants to identify the score occupying a chosen rank after treating both score lists as one ordered collection. Equal scores still occupy separate positions, so the result depends on counting every score.
Problem
Implement find_level_rank. The function receives two nondecreasing arrays of scores and a valid one-based rank. Imagine merging the arrays into one nondecreasing sequence while keeping duplicate scores as separate entries. Return the score at the requested rank. Advance through the arrays with two positions, selecting the smaller available score each time. If the current scores are equal, select from the first array, though either equal value produces the same result. When one array is exhausted, continue counting from the other array. Do not sort or otherwise rearrange the input arrays.
Examples
first_scores = [10, 20, 30]rank = 4second_scores = [15, 25, 35]25first_scores = [2, 2, 5, 9]rank = 3second_scores = [2, 4, 4]2first_scores = [-8, -3, 1]rank = 5second_scores = [6, 12]12Constraints
0 <= len(first_scores) <= 100-100000 <= first_scores[i] <= 100000 for every ifirst_scores: sorted non decreasing0 <= len(second_scores) <= 100-100000 <= second_scores[i] <= 100000 for every isecond_scores: sorted non decreasing1 <= rank <= 200- Types:
first_scoresis int[],second_scoresis int[],rankis int; result is int
Function signature
def find_level_rank(first_scores: list[int], second_scores: list[int], rank: int) -> int
first_scoreslist[int]- Scores from the first level run, in nondecreasing order.
second_scoreslist[int]- Scores from the second level run, in nondecreasing order.
rankint- The one-based position requested in the combined ordering.
- returns int
- The score occupying the requested one-based rank after the two sorted score arrays are merged, with duplicate values counted separately.
Adapted from MBPP problem task_597 (CC BY 4.0). Rewritten, extended and verified by Iksha.
Notes
- The inputs are valid when the requested rank does not exceed their combined length.
- Every occurrence counts separately, including equal scores within one array or across both arrays.