Note furthermore that if you take the sums of the two prefixes, they have the same remainder when divided by 7.
If I have the array [1, 2, 4], the sums of prefix [1, 2] (3) and [1, 2, 4] (7) do not have the same remainder when divided by 7.
I’m also not understanding the rest of the solution in general.
Therefore, for every prefix, we can compute the sum of the numbers in the prefix modulo 7, and keep track of the shortest and longest prefixes that when summed are equivalent to x modulo 7 for 0<x<7. The answer is then the maximum difference between the lengths of the shortest and longest prefixes over all x.
What does “compute sum of the numbers in the prefix modulo 7” mean? And why is the answer the “maximum difference between the lengths of the shortest and longest prefixes”?
This condition holds if and only if the sum of the sublist is divisible by 7.
For example, if you have the array [6,5,1,2,4,3], then [1,2,4] has sum divisible by 7 because the sums of the prefixes [6,5] and [6,5,1,2,4] have the same remainder when divided by 7.
modulo 7 = remainder when divided by 7, see here for info about modular arithmetic.
Say we have the array [1,7,7,7,7,3]. Then the answer is the difference between the lengths of the longest prefix with remainder 1 when divided by 7 ([1,7,7,7,7]) and the shortest ([1]).
Right now you are doing a complete search over the prefix sums. However, this is not necessary. We know that the only way to get a multiple of 7 is if both prefix sums have the same remainder \pmod{ 7}. Then, what happens if there are a lot of these prefix sums with the same remainder. If they’re located at indexes a_1,a_2,a_3,... ,a_n, your O(n^2) solution is finding the maximum of all of the differences.
But we don’t need to check a_2-a_1, for example, since we know a_n-a_1 is divisible by 7, and it must be larger. Thus, we only have to find a_1 and a_n, because all the other differences must be smaller. This means that for each remainder from 0 to 6, we can calculate a_1 and a_n, and find the maximum of their difference, which will be an O(n) solution.