Data Structures & AlgorithmsHardGATE CS · competitive programming
Brian Kernighan's loop (x &= x − 1 until x is 0) counts the set bits of x. What is the total number of loop iterations when it is run once for each x from 1 to 100?
- A. 350
- B. 301
- C. 319
- D. 325
Correct answer: C. 319
Each iteration clears one set bit, so the iterations per number equal its popcount. The sum of popcounts over 1..100 is 319; 350 assumes half of seven bit positions are set for every number.
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
- How many integers x in [0, 1023] have no two adjacent 1 bits in their 10-bit binary form?
- In [1, 2, 1, 3, 2, 5] exactly two values appear once and every other value appears twice. Which two values are they?
- A 10×20 array A[1..10][1..20] is stored column-major. The base address is 1000 and each element takes 8 bytes. What is the address of…
- An n×n matrix is rotated 90° clockwise. The original is [[1,2,3],[4,5,6],[7,8,9]]. Which value is now in the top-left corner?
- A 4×5 matrix holds the value r·5 + c + 1 at row r and column c (0-based). Visited in spiral order from the top-left corner, what is the…
- The 3×4 matrix [[1,3,5,7],[10,11,16,20],[23,30,34,60]] is treated as one sorted array of 12 entries, with mid = (lo + hi) / 2, row = mid /…