Loading...
Loading...
Loading...
Remove the shortest block of consecutive items so that the sum of the remaining items is divisible by p. You may not remove the whole list. Print the length of the block, 0 if no removal is needed, or -1 if it is impossible.
Example
Input
4 6 3 1 4 2
Output
1
Remove [4] and the rest adds to 6.
Try the problem first. Open one hint at a time.
Let r be the total modulo p. You need a block whose sum has remainder r.
Use prefix remainders. For each prefix, look up the latest earlier prefix with remainder (prefix - r) mod p.
Write your Python code, run it on the examples and submit it to hidden tests.
Solve in the editor