Paper Strip
InterviewTime limit1sMemory limit128 MB
Find the longest contiguous block of numbers that adds up to exactly s, or print BRAK when none exists.
- Level
Medium5 of 10
- Topics
- Hash map, Prefix sum
- Solved
- No attempts yet
Problem
Jasio found a very old roll of paper in the attic, on which his great-grandfather had written a sequence of integers. For a game he recently invented, Jasio needs a strip of the paper roll on which the sum of all the numbers is exactly . He will cut away and throw out the parts he does not need.
However, he does not want to throw away too much of his great-grandfather's keepsake, so he wants the strip he keeps to be as long as possible.
Only numbers that lie next to each other on the roll can form a single strip. That is, a strip made of one number has length , a strip made of two neighboring numbers has length , and so on.
Find the length of the longest strip whose sum is exactly .
Input
The first line contains two integers and (, ), the length of the roll and the sum the strip must have.
The second line contains the sequence of integers (), separated by spaces, where is the value of the -th number written on the paper.
Output
Print a single integer, the length of the longest strip whose sum is exactly . If no such strip exists, print the word BRAK instead.