I had this question on an Algorithms test yesterday, and I can\'t figure out the answer. It is driving me absolutely crazy, because it was worth about 40 points. I figure
This may help....
This problem reduces to the following:
Given a sequence of positive integers, find a contiguous subsequence partitioned into a prefix and a suffix such that the sum of the prefix of the subsequence is equal to the sum of the suffix of the subsequence.
For example, given a sequence of [ 3, 5, 1, 3, 6, 5, 2, 2, 3, 5, 6, 4 ]
, we would find a subsequence of [ 3, 6, 5, 2, 2]
with a prefix of [ 3, 6 ]
with prefix sum of 9
and a suffix of [ 5, 2, 2 ]
with suffix sum of 9
.
The reduction is as follows:
Given a sequence of zeros and ones, and starting at the leftmost one, continue moving to the right. Each time another one is encountered, record the number of moves since the previous one was encountered and append that number to the resulting sequence.
For example, given a sequence of [ 0, 1, 1, 0, 0, 1, 0, 0, 0, 1 0 ]
, we would find the reduction of [ 1, 3, 4]
. From this reduction, we calculate the contiguous subsequence of [ 1, 3, 4]
, the prefix of [ 1, 3]
with sum of 4
, and the suffix of [ 4 ]
with sum of 4
.
This reduction may be computed in O(n)
.
Unfortunately, I am not sure where to go from here.