Data Structures & AlgorithmsVery hardGATE CS · TCS NQT
How many integers from 1 to 100 inclusive have exactly two 1-bits in their binary representation?
- A. 15
- B. 21
- C. 28
- D. 7
Correct answer: B. 21
A number with two set bits is 2^a + 2^b with a > b. Bits up to 2⁶ are allowed, and even the largest, 64 + 32 = 96, is ≤ 100, so the count is C(7, 2) = 21. Stopping at bit 5 gives C(6, 2) = 15, using bit 7 gives 28, and 7 counts numbers with one set bit.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- What is the XOR of the elements at indices 2 through 6 (inclusive) of [5, 8, 1, 9, 4, 3, 7, 2]?
- What is the minimum number of bit flips (in a and b combined) to make a OR b equal to c, for a = 2, b = 6, c = 5? (Flip single bits of a…
- How many non-empty subsets of [1, 2, 3, 4, 5] have XOR of their elements equal to 0?
- Intervals [1,3), [2,4), [3,5), [0,6), [5,7), [8,9) may overlap only at endpoints (a touching endpoint is allowed). What is the minimum…
- Five jobs with durations [4, 3, 7, 1, 2] run one at a time on one machine. What is the minimum possible sum of waiting times (the time…
- How many index pairs i < j satisfy 3 divides a[i] + a[j] for a = [1, 2, 3, 4, 5, 6, 7]?