Due to the nature of this problem, you will need to check every number in the files. This means that brute force is the only way to do this. However, there are ways to make it more memory efficient and less prone to memory leaks. The following is a breakdown of a simple algorithm to reduce the complexity of the space in the pseudo-code:
Read n integers into array
buffer[ len(array) ] = int array
total = 0
for integer i in array:
if total % 8 == 0:
total = 0
remove all elements in buffer from array
else
buffer.append(i)
total += i
fi
end for
, , 8, . , 8, , . , , 8. , - O (n). , , .
, . - , ( ) . , n m 8, f (n% 8) + (m% 8) == 0,8. : 5 3, 17 15, 12 12.
, , , O (n), , .