Find the Largest Almost Missing Integer
Real-world version: Scarce Colour CodeThe statement here is our own: the same inputs, outputs and technique.
A paint line stamps one colour code onto every crate that passes. The array codes lists those stamps in the order the crates went by, and the auditor inspects the line in blocks: a block is any run of k consecutive entries of codes, so there are codes.length - k + 1 blocks in total and blocks may overlap.
A colour code is called scarce when exactly one block contains it. A block contains a code if the code is stamped on at least one of that block's k crates; a block that carries the same code twice still counts as one block.
Return the largest scarce colour code. Every colour code is non-negative, so return -1 when no code is scarce.
Examples
Example 1
- Input
- codes = [5, 1, 1, 2], k = 2
- Output
- 5
The three blocks are the first two crates, the middle two crates and the last two crates. Code 5 turns up only in the first block and code 2 only in the last, while code 1 turns up in all three, so the largest scarce code is 5.
Example 2
- Input
- codes = [3, 9, 3], k = 2
- Output
- -1
The two blocks are the first two crates and the last two crates. Code 3 is carried by both blocks and so is code 9, so nothing is scarce.
Example 3
- Input
- codes = [12, 50, 4, 50], k = 4
- Output
- 50
The block width matches the number of crates, so there is a single block and it carries all three codes. Each of them is therefore scarce, and 50 is the largest.
Constraints
- 1 <= codes.length <= 50
- 0 <= codes[i] <= 50
- 1 <= k <= codes.length
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def largest_scarce_code(codes: list[int], k: int) -> int:public int largestScarceCode(int[] codes, int k)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 14 test cases: 14 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's