Trains the technique from
LeetCode 3026Maximum Good Subarray SumThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A ledger holds the signed amounts of a day's entries in the list entries; a credit is positive and a debit is negative.
An auditor works on matched blocks. A matched block is a run of consecutive entries in which the amount of the first entry and the amount of the last entry differ by exactly gap, in either direction. Because gap is at least 1, a matched block always holds at least two entries.
Return the largest total a matched block can have. Return 0 when the ledger has no matched block at all; note that a matched block whose entries happen to total exactly 0 produces the same return value, so the two situations cannot be told apart from the answer alone.
Example 1
The whole ledger is a matched block: it starts at 3, ends at 9, and those differ by 6. Its total is 24.
Example 2
Entries 1 to 3 form a matched block, running 4, -100, 6, since 4 and 6 differ by 2, and it totals -90. Entries 3 to 5 also form one and also total -90.
Example 3
The only run of two or more entries starts at 7 and ends at 3, which differ by 4 rather than 5, so there is no matched block and the answer is 0.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def best_matched_block(entries: list[int], gap: int) -> int:public long bestMatchedBlock(int[] entries, int gap)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.