Last Score in a Combo Class
Context
In the arcade game Neon Vault, a level awards bonus scores from 0 up to a level's score limit. The leaderboard groups scores by their remainder when divided by a combo modulus. Find the highest possible bonus score in the requested group.
Problem
Write a function that finds the greatest integer score from 0 through score_limit, inclusive, whose remainder when divided by modulus equals target_remainder. If no score in that range has the requested remainder, return -1. The modulus is positive, and the requested remainder is always a valid remainder for that modulus. Do not enumerate every score; use the structure of the repeating remainder classes to locate the answer directly.
Examples
modulus = 6score_limit = 20target_remainder = 220score_limit = 20, modulus = 6, and target_remainder = 2, the executed result is 20. The final candidate and best value are both 20.modulus = 7score_limit = 25target_remainder = 021score_limit = 25, modulus = 7, and target_remainder = 0, the executed result is 21. The final candidate and best value are both 21. (,, )modulus = 9score_limit = 4target_remainder = 6-1score_limit = 4, modulus = 9, and target_remainder = 6, the executed result is -1. The final candidate and best value are both -3, and the no-qualifying-score result is -1. (,, )Constraints
0 <= score_limit <= 10000001 <= modulus <= 10000000 <= target_remainder <= modulus - 1- Types:
score_limitis int,modulusis int,target_remainderis int; result is int
Function signature
def last_combo_score(score_limit: int, modulus: int, target_remainder: int) -> int
score_limitint- The inclusive maximum bonus score available in the level.
modulusint- The positive number used to divide scores into repeating combo classes.
target_remainderint- The remainder class whose greatest score should be found.
- returns int
- The greatest score from 0 through score_limit whose remainder modulo modulus equals target_remainder, or -1 when no such score exists.
Adapted from MBPP problem task_663 (CC BY 4.0). Rewritten, extended and verified by Iksha.
Notes
- Scores include zero, and the upper limit is inclusive.
- A valid remainder can still have no matching score when the limit is too small.