Skip to content
Related Articles

Related Articles

Improve Article
Count odd and even Binomial Coefficients of N-th power
  • Last Updated : 05 May, 2021

Given an integer N, the task is to count the number of even and odd binomial coefficients up to Nth power.

Examples:

Input: N = 4
Output:
Odd: 2
Even: 3
Explanation:
The binomial coefficients are as follows:
4C0 = 1, 4C1 = 4 , 4C2 = 6 , 4C3 = 4 , 4C4 = 1.
Therefore, it can be observed that there exists exactly 2 odd and 3 even Binomial Coefficients.

Input: N = 5
Output:
Odd: 4
Even: 2
Explanation:
The binomial coefficients are as follows:
5C0 = 1, 5C1 = 5, 5C2 = 10, 5C3 = 10, 5C4 = 5, 5C5 = 1.
Therefore, there are 4 odd and 2 even coefficients.

Solution Approach: The idea to solve this problem is using Bit Manipulation. Find the set bits in the given integer N. Count of odd binomial coefficients are equal to 2 ^ Count of Set Bits in N. Similarly the count of even binomial coefficients is equal to (N + 1 – 2 ^ Count of Set Bits in N).



Below is the implementation of the above approach:

C++




// C++ program for the above approach
 
#include <iostream>
#include <math.h>
using namespace std;
 
// Function to count set bits in
// binary representation of number N
int countSetBits(int N)
{
    int count = 0;
 
    // Count set bits in N
    while (N) {
 
        N = N & (N - 1);
        count++;
    }
 
    // Return the final count
    return count;
}
 
// Driver Code
int main()
{
    int N = 4;
 
    int bits = countSetBits(N);
 
    // Print odd Binomial coefficents
    cout << "Odd "
         << ": " << pow(2, bits) << "\n";
 
    // Print even Binomial coefficents
    cout << "Even "
         << ": " << N + 1 - pow(2, bits)
         << "\n";
 
    return 0;
}

Java




// Java program for the above approach
import java.util.*;
  
class GFG{
      
// Function to count set bits in
// binary representation of number N
static int countSetBits(int N)
{
    int count = 0;
  
    // Count set bits in N
    while (N != 0)
    {
         
        N = N & (N - 1);
        count++;
    }
  
    // Return the final count
    return count;
}
  
// Driver code
public static void main(String[] args)
{
    int N = 4;
  
    int bits = countSetBits(N);
  
    // Print odd Binomial coefficents
    System.out.println("Odd " + ": " +
               (int)(Math.pow(2, bits)));
  
    // Print even Binomial coefficents
    System.out.println("Even " + ": " +
               (N + 1 - (int)(Math.pow(2, bits))));
}
}
 
// This code is contributed by susmitakundugoaldanga

Python3




# Python3 program for the above approach
 
# Function to count set bits in
# binary representation of number N
def countSetBits(N: int) -> int:
 
    count = 0
 
    # Count set bits in N
    while (N):
        N = N & (N - 1)
        count += 1
 
    # Return the final count
    return count
 
# Driver Code
if __name__ == "__main__":
 
    N = 4
 
    bits = countSetBits(N)
 
    # Print odd Binomial coefficents
    print("Odd : {}".format(pow(2, bits)))
 
    # Print even Binomial coefficents
    print("Even : {}".format(N + 1 - pow(2, bits)))
 
# This code is contributed by sanjeev2552

C#




// C# program for the above approach
using System;
  
class GFG{
  
// Function to count set bits in
// binary representation of number N
static int countSetBits(int N)
{
    int count = 0;
   
    // Count set bits in N
    while (N != 0)
    {
        N = N & (N - 1);
        count++;
    }
   
    // Return the final count
    return count;
}
  
// Driver Code
public static void Main()
{
    int N = 4;
    int bits = countSetBits(N);
   
    // Print odd Binomial coefficents
    Console.WriteLine("Odd " + ": " +
                     (int)(Math.Pow(2, bits)));
   
    // Print even Binomial coefficents
    Console.WriteLine("Even " + ": " +
                     (N + 1 - (int)(Math.Pow(2, bits))));
}
}
 
// This code is contributed by sanjoy_62

Javascript




<script>
 
// Javascript program for the above approach
 
// Function to count set bits in
// binary representation of number N
function countSetBits(N)
{
    let count = 0;
  
    // Count set bits in N
    while (N != 0)
    {
        N = N & (N - 1);
        count++;
    }
  
    // Return the final count
    return count;
}
   
// Driver Code
let N = 4;
 
let bits = countSetBits(N);
 
// Print odd Binomial coefficents
document.write("Odd " + ": " +
              (Math.pow(2, bits)) + "<br/>");
 
// Print even Binomial coefficents
document.write("Even " + ": " +
              (N + 1 - (Math.pow(2, bits))));
               
// This code is contributed by splevel62
 
</script>
Output: 
Odd : 2
Even : 3

 

Time Complexity: O(1)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :