Data Structures & AlgorithmsVery hardGATE CS · TCS NQT
What is the minimum number of operations needed to reduce 191 to 0, where each operation subtracts any single power of two that is at most the current value?
- A. 8
- B. 7
- C. 2
- D. 1
Correct answer: B. 7
Each subtraction of a power of two can clear at most one 1-bit, and clearing one bit at a time works, so the answer is the popcount: 191 = 10111111₂ has 7 ones. The bit length is 8, 1 counts the zero bits, and 2 imagines 192 − 1, which needs an addition.
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 sum of all integers from 1 to 100 inclusive that are divisible by neither 3 nor 7?
- How many windows of length 3 in [1, 2, 1, 2, 3, 3, 1] contain exactly two distinct values?
- What is the floor of the square root of the sum of [1000, 2500, 900]?
- How many indices i in "xyzxyzxyz" satisfy s[i] = s[i + 3]?
- A singly linked list holds 1..7. Using the slow/fast pointer method, the slow pointer reaches the middle node. How many nodes come after…
- What is the minimum number of elements to delete from [1, 1, 1, 2, 2, 2, 2, 3] so that every value appears at most twice?