PracticeMath

Last Score in a Combo Class

EasyMathmodular arithmeticboundary handlingcomparison Time not estimated Not started

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

Example 1
Input: modulus = 6score_limit = 20target_remainder = 2
Output: 20
Explanation: For score_limit = 20, modulus = 6, and target_remainder = 2, the executed result is 20. The final candidate and best value are both 20.
Example 2
Input: modulus = 7score_limit = 25target_remainder = 0
Output: 21
Explanation: For score_limit = 25, modulus = 7, and target_remainder = 0, the executed result is 21. The final candidate and best value are both 21. (,, )
Example 3
Input: modulus = 9score_limit = 4target_remainder = 6
Output: -1
Explanation: For score_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 <= 1000000
  • 1 <= modulus <= 1000000
  • 0 <= target_remainder <= modulus - 1
  • Types: score_limit is int, modulus is int, target_remainder is int; result is int

Function signature

def last_combo_score(score_limit: int, modulus: int, target_remainder: int) -> int
score_limit int
The inclusive maximum bonus score available in the level.
modulus int
The positive number used to divide scores into repeating combo classes.
target_remainder int
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.
Approved · cc017-abs_63_v114 execution-verified tests
Code
Saved
Checking environment…Python SandboxLn 1, Col 1