Given N, the row number of Pascal’s triangle(row starting from 0). Find the count of odd numbers in N-th row of Pascal’s Triangle.
Input : 11 Output : 8 Input : 20 Output : 4
Approach : It appears the answer is always a power of 2. In fact, the following theorem exists :
THEOREM : The number of odd entries in row N of Pascal’s Triangle is 2 raised to the number of 1’s in the binary expansion of N.
Example: Since 83 = 64 + 16 + 2 + 1 has binary expansion (1010011), then row 83 has pow(2, 4) = 16 odd numbers.
Below is the implementation of above approach :
Time Complexity : O(L), where L is the length of binary representation of given N.
Reference : https://www.math.hmc.edu/funfacts/ffiles/30001.4-5.shtml
- Pascal's Triangle
- Calculate nCr using Pascal's Triangle
- Leibniz harmonic triangle
- Sum of all elements up to Nth row in a Pascal triangle
- XNOR of two numbers
- Equal Sum and XOR of three Numbers
- Find two numbers from their sum and XOR
- Sum of numbers with exactly 2 bits set
- Bitwise AND of all the odd numbers from 1 to N
- Maximum XOR using K numbers from 1 to n
- Count numbers whose XOR with N is equal to OR with N
- Check whether product of 'n' numbers is even or odd
- Find k numbers which are powers of 2 and have sum N | Set 1
- Check whether bitwise OR of N numbers is Even or Odd
- Find XOR of numbers from the range [L, R]
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.