Loading...
Loading...
Loading...
Find the length of the longest block of consecutive items whose sum is exactly k. Print 0 if there is none. Items can be negative.
Example
Input
5 3 1 -1 5 -2 3
Output
4
The first four items add to 3.
Try the problem first. Open one hint at a time.
Use prefix sums again, but store where each prefix first appeared.
Never overwrite a stored index. The earliest start gives the longest block.
Write your Python code, run it on the examples and submit it to hidden tests.
Solve in the editor