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?
- A. 3
- B. 4
- C. 1
- 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
- Ropes of lengths 4, 3, 2 and 6 are joined two at a time; joining ropes of lengths x and y costs x + y and creates one rope of length x +…
- Array [1, 2, 4, 1, 3, 4]. How many contiguous subarrays have a bitwise OR of exactly 7?
- Set A = {1, 2, 4, 7} and set B = {3, 5, 6, 8}. Choose two different elements from A and two different elements from B. How many choices…
- Half-open intervals [0,2), [1,3), [2,4), [3,5), [4,6). What is the minimum number of intervals to remove so the remaining intervals are…
- Array a = [2, 3, 5, 1, 4, 5, 2, 3]. How many contiguous subarrays have an even length and a sum divisible by 5?
- In the binary string "1101001100", find the length of the longest substring that has equally many 0s and 1s and whose length is a multiple…