Given an integer N, the task is to print the number of binary strings of length N which at least one ‘1’.
“01”, “10” and “11” are the possible strings
“001”, “011”, “010”, “100”, “101”, “110” and “111” are the possible strings
We can observe that:
Only one string of length N does not contain any 1, the one filled with only 0’s.
Since 2N strings are possible of length N, the required answer is 2N – 1.
Follow the steps below to solve the problem:
- Initialize X = 1.
- Compute upto 2N by performing bitwise left shift on X, N-1 times.
- Finally, print X – 1 as the required answer.
Below is the implementation of our approach:
Time Complexity: O(N)
Auxiliary Space: O(1)
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.
- Maximum count of sub-strings of length K consisting of same characters
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Count of binary strings of length N with even set bit count and at most K consecutive 1s
- Count of binary strings of length N having equal count of 0's and 1's
- Count number of binary strings of length N having only 0's and 1's
- Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1's
- Count of Binary strings of length N having atmost M consecutive 1s or 0s alternatively exactly K times
- Count of N size strings consisting of at least one vowel and one consonant
- Count of Binary Strings of length N such that frequency of 1's exceeds frequency of 0's
- Count of same length Strings that exists lexicographically in between two given Strings
- Number of binary strings such that there is no substring of length ≥ 3
- Number of Binary Strings of length N with K adjacent Set Bits
- Generate Binary Strings of length N using Branch and Bound
- Find the number of binary strings of length N with at least 3 consecutive 1s
- Generate all binary strings of length n with sub-string "01" appearing exactly twice
- Count of sub-strings of length n possible from the given string
- Count of non-palindromic strings of length M using given N characters
- Count the number of Special Strings of a given length N
- Count ways to increase LCS length of two strings by one
- Count of unique palindromic strings of length X from given string
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.