Data Structures & AlgorithmsModerateCodeforces Div 2 C · ICPC regional
What does this program print?
What does this program print?
x = 22
cnt = 0
while x:
x &= x - 1
cnt += 1
print(cnt, bin(22).count('1'))- A. 5 3
- B. 3 2
- C. 3 3
- D. 2 3
Correct answer: C. 3 3
The statement x &= x - 1 clears the lowest set bit each time, so the loop runs once per set bit. The first value equals the popcount of 22, which is 3, and the program prints it next to the built-in count.
More Coding Interview Problems questions
- Array [2, 7, 4, 9, 5, 8]. What is the largest sum of a contiguous subarray having at most 3 elements?
- Points (0,0), (1,2), (2,1), (3,3), (4,0) in the plane. How many unordered pairs have Manhattan distance exactly 3?
- Four sets {1,2,3}, {2,4}, {3,4,5}, {1,5} are given. How many non-empty sub-collections of these sets have a union equal to {1, 2, 3, 4, 5}?
- Problem: How many pairs of equal values (i < j, a[i] = a[j]) does a = [1, 2, 1, 3, 2, 2] contain? Run the code. python from collections…
- Problem: How many longest increasing subsequences does [1, 3, 5, 4, 7] have? Run the DP that counts both length and number of optimal…
- Problem: What is the largest number of distinct characters in any window of length 3 of 'abcabc'? Run the code. python s = 'abcabc'…