Odd numbers in N-th row of Pascal’s Triangle


The problem statement includes counting the odd numbers in N−th row of Pascal’s triangle.

A pascal’s triangle is a triangular array where each row represents the binomial coefficients in the expansion of binomial expression. The Pascal’s triangle is demonstrated as below:

                              1
                          1        1
                      1        2        1
                   1      3         3       1
               1       4       6        4      1

The triangle can be further expanded with the same logic. Every value in Pascal's triangle represents the binomial coefficient starting from n=0 as rows and every value in the row represents $\mathrm{^nC_{r}}$, where r ranges from r=0 to r=n.

Note: For every N−th row, there are total (N+1) terms.

In this problem, we will be provided a number N in the input and our task will be to count the number of odd numbers in the N−th row of the Pascal’s triangle.

Let’s understand the problem with the below examples.

Input

N=5

Output

4

Explanation − The input number is 5. There will be a total of 6 terms in the 5th row of Pascal’s triangle i.e. 1, 5, 10, 10, 5 and 1 which can be either calculated using the formula $\mathrm{^5C_{r}}$ where r ranges from 0 to 5 or using the Pascal’s triangle.

The number of odd terms in the 5th row of Pascal’s triangle are 4 i.e. 1, 5, 5 and 1 which is the required output.

Input

N=10

Output

4

Explanation − The input given is 10 which means we need to count the odd numbers in the 10th row of Pascal’s triangle. The value of binomial coefficients in the 10th row will be 1, 10, 45, 120, 210, 252, 210, 120, 45, 10 and 1. The number of odd terms is 4, which is the required output.

Let’s understand the algorithm to count the number of odd numbers in any N−th row of the Pascal’s triangle.

Algorithm

There is a mathematical relation which gives the number of odd numbers in the N−th row of pascal’s triangle. The theorem states that the number of odd numbers in N−th row is equal to 2 raised to the number of ones in the binary representation of N.

Let’s understand the theorem with an example.

Let’s say we need to count the odd numbers in the 10th row of Pascal's triangle. The binary representation of 10 is 1010 i.e. $\mathrm{2^{3}+2^{1}=8+2=10}$ .The number of 1’s in binary representation of 10 is 2.

According to the theorem, the number of odd numbers in the N−th row of the Pascal’s triangle will be equal to the 2 raised to number of 1’s in binary representation of N,

Number of odd numbers in 10th row $\mathrm{2^{2}=4}$

We will use the above theorem in our approach in order to count the number of odd numbers in the N−th row of the Pascal’s triangle.

Approach

The steps to follow to implement the algorithm in our approach in order to get the count of odd numbers:

  • We will make a function to count the number of 1’s in the binary representation of N.

  • Initialise a variable to account for the number of 1’s. Then, we will iterate in a while loop until N is greater than 0 where we will update the count by taking AND of N and 1 as it returns 1 only if both the bits are 1 else the operator returns 0. Meanwhile, we will keep updating N by right shift(>>) by 1.

  • Once we get the number of 1’s in the binary representation of N, we will find the count of odd numbers by calculating 2 raised to the number of 1’s using pow() function.

  • Return the value of $\mathrm{2^{no\:of\:1's}}$ which will be the required output.

Example

//C++ code to find the number of odd numbers in the N-th row of Pascal's Triangle

#include <bits/stdc++.h>

using namespace std;

//function to find the number of set bits in binary representation of N
int numberofones(int N){
    int a=0;  //to store the number of set bits
    //iterating in the loop to count the set bits
    while(N>0){ 
        
        a = a + (N&1);  //update a by adding 1 if there is a set bit in N
        
        N = N>>1;  //right shift N by 1 to check for other set bits
    }
    
    return a;  //return number of 1's in N
}

//function to get the count of number of odd numbers in N-th row
int numberofodds(int N){
    int x;  //to store the number of set bits of N
    x=numberofones(N);  //calling the function to store number of set bits of N in x 
    
    int ans;  //to store count of odd numbers
    ans=pow(2,x);  //number of odd numbers equal to the 2 raised to no of set bits in N
    
    return ans; //return the count
}

int main()
{
    int N;  //for taking the input
    N=25;
    //calling the function
    cout<<"Count of odd numbers in the "<<N<<"th row of Pascal's triangle : "<<numberofodds(N)<<endl;
    
    N=53;
    cout<<"Count of odd numbers in the "<<N<<"th row of Pascal's triangle : "<<numberofodds(N)<<endl;

    return 0;
}

Output

Count of odd numbers in the 25th row of Pascal's triangle : 8
Count of odd numbers in the 53th row of Pascal's triangle : 16

Time complexity: O(N) time taken to count number of set bits of N.

Space complexity: O(N) because no extra space is taken to find the count of odd numbers.

Conclusion

The problem of counting the odd numbers in N−th row of the Pascal’s triangle was discussed in the article. We solved the problem with an efficient approach in C++ using the theorem in mathematics for the number of odd numbers in the N−th row of Pascal’s triangle.

I hope you understand the problem and the approach after reading this article.

Updated on: 28-Aug-2023

87 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements