Data Structures & AlgorithmsModerateProduct Company SDE · ICPC regional
Count the subarrays of v = [4, 6, 2, 1, 7, 3, 5, 1] whose XOR equals 0, considering only subarrays of length at least 3. Brute-force all subarrays.
Count the subarrays of v = [4, 6, 2, 1, 7, 3, 5, 1] whose XOR equals 0, considering only subarrays of length at least 3. Brute-force all subarrays.
from functools import reduce v=[4,6,2,1,7,3,5,1] n=len(v) print(sum(1 for i in range(n) for j in range(i+2,n) if reduce(lambda a,b:a^b,v[i:j+1],0)==0))What is printed?
- A. 4
- B. 1
- C. 0
- D. 7
Correct answer: A. 4
A subarray has XOR 0 when its prefix XORs match, and the brute force checks each subarray directly. Testing the sum instead of the XOR is a different condition, and targeting XOR 1 is a misread. The length-three restriction is applied to the scan range, so it must start at i + 2.
More Coding Interview Problems questions
- Histogram bars have heights h = [2, 1, 5, 6, 2, 3]. What is the area of the largest rectangle that fits entirely inside the histogram…
- What is the maximum sum of a non-empty subarray in the circular array [8, -8, 9, -9, 10, -11, 12], where the subarray may wrap from the…
- What is the maximum product of a non-empty contiguous subarray of [2, -5, -2, -4, 3]?
- What is the minimum number of coins needed to make an amount of 18 using unlimited coins of denominations {1, 6, 10}?
- The array is [3, 3, 4, 4, 5, 1, 2, 2, 6, 3, 7]. What is the length of its longest non-decreasing (not strictly increasing) subsequence?
- For the circular array [5, -3, 5, -2, 6, -8, 4], what is the maximum sum of a non-empty contiguous subarray when the subarray is allowed…