Subsequences Summing to Sevens
InterviewTime limit2sMemory limit512 MB
Find the length of the longest contiguous group of cows whose IDs sum to a multiple of 7.
- Level
Medium4 of 10
- Topics
- Prefix sum, Hash map
- Solved
- No attempts yet
Problem
Farmer John's cows are standing in a row. Every cow carries a distinct integer ID, which is how John tells them apart. He wants to photograph a contiguous group of cows, but because of a childhood incident involving the numbers through , he only photographs a group whose IDs add up to a multiple of .
Find how many cows are in the largest group John can photograph.
Input
The first line contains (). Each of the next lines contains one cow ID, in the order the cows stand. Every ID is an integer between and .
Output
Print the number of cows in the longest contiguous group whose IDs sum to a multiple of . If no such group exists, print .
The IDs of a long group can add up to more than a 32-bit integer holds. If you add the IDs directly, use a 64-bit type such as long long in C or C++.
Hint
In the example, is a multiple of , so the answer is .