Given a positive integer N, the task is to count the numbers that can be represented with N bits and whose 0th and Nth bits are set.
Input: N = 2
All possible 2-bit integers are 00, 01, 10 and 11.
Out of which only 11 has 0th and Nth bit set.
Input: N = 4
Approach: Out of the given N bits, only two bits need to be set i.e. the 0th and the Nth bit. So, setting these 2 bits as 1 we are left with the rest N – 2 bits every single of which can either be 0 or 1 and there are 2N – 2 ways of doing that.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with industry experts, please refer DSA Live Classes