Data Structures & AlgorithmsModerateCodeforces Div2 C/D · Amazon SDE OA
Count the pairs (a, b) with 1 ≤ a < b ≤ 20 such that a OR b (bitwise) equals 31. Run the code.
Count the pairs (a, b) with 1 ≤ a < b ≤ 20 such that a OR b (bitwise) equals 31. Run the code.
print(sum(1 for a in range(1,21) for b in range(a+1,21) if a|b==31))What is printed?
- A. 11
- B. 64
- C. 0
- D. 5
Correct answer: A. 11
Bitwise OR equals 31 only when the two values cover all five bits, and a < b excludes the equal pair. AND with zero is a different condition, AND equal to 31 is impossible for these values, and requiring a plus b to equal 31 is a different constraint.
More Coding Interview Problems questions
- Floyd–Warshall is run on the 4-vertex directed weighted graph with matrix rows [0,3,∞,7], [8,0,2,∞], [5,∞,0,1], [2,∞,∞,0]. Sum the final…
- A rod of length 8 can be cut into integer pieces, and a piece of length L sells for p[L] with p = [1, 5, 8, 9, 10, 17, 17, 20] for L =…
- Array b = [1, 11, 2, 10, 4, 5, 2, 1]. Find the length of its longest bitonic subsequence: a subsequence that strictly increases and then…
- Count the 4-element subsets of {1, 2, ..., 12} whose sum is divisible by 5. Count by brute force over combinations. python from itertools…
- Find the smallest positive integer x such that x ≡ 2 (mod 3), x ≡ 3 (mod 5) and x ≡ 2 (mod 7). Solve by brute-force search. python…
- How many trailing zeros does 100! have in base 10? Use Legendre's formula with powers of 5, counted by running the loop over p = 5, 25…