Data Structures & AlgorithmsHardCodeforces Div2 C · GATE CS
What is the value of sum of popcount(i) for i from 1 to 20 (number of 1-bits in the binary form of each i, summed)?
- A. 40
- B. 43
- C. 45
- D. 42
Correct answer: D. 42
Using the recurrence popcount(i) = popcount(i >> 1) + (i & 1), or summing directly, gives 42 for 1 to 20. Dropping the term for 20 itself gives 40.
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
- For the array [6, 12, 5, 8], what is the sum of (x & -x) over all elements x, where x & -x is the lowest set bit of x?
- How many contiguous subarrays of [1, 2, 4, 3, 6] have a bitwise OR equal to exactly 7?
- How many distinct non-empty substrings does the string "abacaba" have?
- In the string "ababababa", how many times does "aba" occur, counting overlapping occurrences?
- For the string "aabaaab", what is the last value of the KMP prefix-function (the length of the longest proper prefix that is also a suffix…
- What is the smallest period p (1 <= p < n) of the string "abcabcabca" such that s[i] = s[i + p] for all valid i?