Data Structures & AlgorithmsModerateCodeforces Div2 B · bit counting
Problem: What is the sum over all pairs (i < j) of the Hamming distances (number of differing bits) between the values in [1, 3, 5]? Run the code.
Problem: What is the sum over all pairs (i < j) of the Hamming distances (number of differing bits) between the values in [1, 3, 5]? Run the code.
a = [1, 3, 5]
print(sum(bin(a[i] ^ a[j]).count('1') for i in range(len(a)) for j in range(i + 1, len(a))))What is printed?- A. 3
- B. 5
- C. 6
- D. 4
Correct answer: D. 4
1 XOR 3 = 2 (one bit), 1 XOR 5 = 4 (one bit) and 3 XOR 5 = 6 (two bits), giving 4. The sum of XOR values is a different quantity.
More Coding Interview Problems questions
- Problem: People with weights [3, 2, 2, 1] must cross a river. A boat carries at most 2 people with total weight at most limit = 3, and…
- Problem: Koko eats bananas from piles [3, 6, 7, 11] and has h = 8 hours. She picks one pile per hour and eats up to k bananas from it…
- Problem: How many primes are there up to 100? Run the sieve. python n = 100 sieve = [True] (n + 1) sieve[0] = sieve[1] = False for i in…
- Problem: How many ways can the digit string '226' be decoded when 1 maps to A, 2 to B, ..., 26 to Z? Run the DP. python s = '226' dp = [1]…
- Problem: Union-find with path compression and union by rank performs m operations on n elements in what amortised time per operation?
- Problem: What is the largest rectangle area that can be formed in a histogram of heights [2, 1, 5, 6, 2, 3]? Use the stack code. python h…