Count of N-bit binary numbers without leading zeros
Given an integer N, the task is to find the count of N-bit binary numbers without leading zeros.
Input: N = 2
10 and 11 are the only possible binary numbers.
Input: N = 4
Approach: Since the numbers cannot have leading zeros so the left-most bit has to be set to 1. Now for the rest of the N – 1 bits, there are two choices they can either be set to 0 or 1. So, the count of possible numbers will be 2N – 1.
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.