I came across this post on stackoverflow on january’15 long challenge problem QSET.But there is one thing I don’t understand in the merge function:
Node merge(Node left, Node right) {
Node res = new Node();
// The total sum is just the sum of the merged nodes.
res.totalSum = (left.totalSum + right.totalSum) % 3;
for (int i = 0; i < 3; i++) {
res.withRemainder[i] += left.withRemainder[i];
// Sums in the right node are shifted by left.totalSum
res.withRemainder[(i + left.totalSum) % 3] += right.withRemainder[i];
}
return res;
}
Should’nt there be a statement like:
res.withRemainder[i] += right.withRemainder[i]
in the for loop.Because we need a substring and that can be:
(i)left node string
(ii)left node string + right node string
(iii)right node string
otherwise we won’t be considering right node string alone.