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:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Count total set bits in all numbers from 1 to n | Set 2
- Count total unset bits in all the numbers from 1 to N
- Count set bits in the Kth number after segregating even and odd from N natural numbers
- Count pairs with set bits sum equal to K
- Count unset bits in a range
- Count pairs (A, B) such that A has X and B has Y number of set bits and A+B = C
- Count of elements in an Array whose set bits are in a multiple of K
- Count number of set bits in a range using bitset
- Count of divisors having more set bits than quotient on dividing N
- Minimum number N such that total set bits of all numbers from 1 to N is at-least X
- Find a number X such that (X XOR A) is minimum and the count of set bits in X and B are equal
- Number of ways to change the XOR of two numbers by swapping the bits
- Count of total bits toggled/flipped in binary representation of 0 to N
- Count numbers < = N whose difference with the count of primes upto them is > = K
- Count numbers which are divisible by all the numbers from 2 to 10
- Count numbers which can be constructed using two numbers
- Count numbers that don't contain 3
- Count Odd and Even numbers in a range from L to R
- Count numbers with same first and last digits
- Count of all N digit numbers such that num + Rev(num) = 10^N - 1
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.