Data Structures & AlgorithmsHardCodeforces Div 2 C · ICPC regional

Array [1, 2, 4, 3, 0, 5, 6]. What is the length of the longest contiguous subarray in which the XOR of the elements equals their sum?

  1. A. 3
  2. B. 4
  3. C. 1
  4. D. 5

Correct answer: A. 3

XOR equals sum exactly when there are no carries, i.e. the elements share no set bits pairwise. A subarray with pairwise disjoint bits satisfies the condition, so a two-pointer window over the array finds the longest one.

More Coding Interview Problems questions

Exams that ask this topic