1435. Odd Entries in Pascal Row
Row n of Pascal's triangle lists the binomial coefficients C(n,0), C(n,1), ..., C(n,n), and row 0 is just 1. Return how many of those n + 1 entries are odd, as a long. The row is far too long to generate for large n, so find the count from the binary representation of n: the answer is two raised to the number of set bits in n. The answer never exceeds 2^50, so it fits easily in a 64-bit integer. Your solution should take O(log n) time and O(1) space.
Example 1
- Input:
- n = 7
- Output:
- 8
- Explanation:
Row 7 holds 8 entries and every one of them is odd, matching 2^3.
Example 2
- Input:
- n = 8
- Output:
- 2
- Explanation:
Row 8 has only its two ends (1 and 1) odd, matching 2^1.
Constraints
0 ≤ n ≤ 1015
The answer is at most 250.
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Expected complexity
- Time
- O(log n)
- Space
- O(1)