Count of binary strings of given length consisting of at least one 1 Last Updated : 03 Aug, 2021 Comments Improve Suggest changes Like Article Like Report Given an integer N, the task is to print the number of binary strings of length N which at least one '1'. Examples: Input: 2 Output: 3 Explanation: "01", "10" and "11" are the possible strings Input: 3 Output: 7 Explanation: "001", "011", "010", "100", "101", "110" and "111" are the possible strings Approach: 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: C++ // C++ Program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to return // the count of strings long count_strings(long n) { int x = 1; // Calculate pow(2, n) for (int i = 1; i < n; i++) { x = (1 << x); } // Return pow(2, n) - 1 return x - 1; } // Driver Code int main() { long n = 3; cout << count_strings(n); return 0; } Java // Java program to implement // the above approach import java.util.*; class GFG{ // Function to return // the count of Strings static long count_Strings(long n) { int x = 1; // Calculate Math.pow(2, n) for(int i = 1; i < n; i++) { x = (1 << x); } // Return Math.pow(2, n) - 1 return x - 1; } // Driver Code public static void main(String[] args) { long n = 3; System.out.print(count_Strings(n)); } } // This code is contributed by Amit Katiyar Python3 # Python3 program to implement # the above approach # Function to return # the count of Strings def count_Strings(n): x = 1; # Calculate pow(2, n) for i in range(1, n): x = (1 << x); # Return pow(2, n) - 1 return x - 1; # Driver Code if __name__ == '__main__': n = 3; print(count_Strings(n)); # This code is contributed by Princi Singh C# // C# program to implement // the above approach using System; class GFG{ // Function to return // the count of Strings static long count_Strings(long n) { int x = 1; // Calculate Math.Pow(2, n) for(int i = 1; i < n; i++) { x = (1 << x); } // Return Math.Pow(2, n) - 1 return x - 1; } // Driver Code public static void Main(String[] args) { long n = 3; Console.Write(count_Strings(n)); } } // This code is contributed by Amit Katiyar JavaScript <script> // Javascript program to implement // the above approach // Function to return // the count of Strings function count_Strings(n) { var x = 1; // Calculate Math.pow(2, n) for (i = 1; i < n; i++) { x = (1 << x); } // Return Math.pow(2, n) - 1 return x - 1; } // Driver Code var n = 3; document.write(count_Strings(n)); // This code is contributed by todaysgaurav </script> Output: 3 Time Complexity: O(N) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Count of binary strings of given length consisting of at least one 1 R RitvikSangwan Follow Improve Article Tags : Strings Bit Magic Algorithms Dynamic Programming Combinatorial DSA binary-string +3 More Practice Tags : AlgorithmsBit MagicCombinatorialDynamic ProgrammingStrings +1 More Similar Reads Count N-length Binary Strings consisting of "11" as substring Given a positive integer N, the task is to find the number of binary strings of length N which contains "11" as a substring. Examples: Input: N = 2Output: 1Explanation: The only string of length 2 that has "11" as a substring is "11". Input: N = 12Output: 3719 Approach: The idea is to derive the num 8 min read Count of groups of consecutive 1s in a given Binary String Given a binary string S of size N, the task is to find the number of groups of 1s only in the string S. Examples: Input: S = "100110111", N = 9Output: 3Explanation: The following groups are of 1s only: Group over the range [0, 0] which is equal to "1".Group over the range [3, 4] which is equal to "1 5 min read Count of K length subarrays containing only 1s in given Binary String Given a binary string str, the task is to find the count of K length subarrays containing only 1s. Examples: Input: str = "0101000", K=1Output: 2Explanation: 0101000 -> There are 2 subarrays with 1 ones Input: str = "11111001", K=3Output: 3 Approach: The task can be solved by keeping track of the 4 min read Count of binary strings of length N having equal count of 0's and 1's Given an integer N, the task is to find the number of binary strings possible of length N having same frequency of 0s and 1s. If such string is possible of length N, print -1. Note: Since the count can be very large, return the answer modulo 109+7.Examples: Input: N = 2 Output: 2 Explanation: All po 6 min read Count of K length subarrays containing only 1s in given Binary String | Set 2 Given binary string str, the task is to find the count of K length subarrays containing only 1s. Examples Input: str = "0101000", K=1Output: 2Explanation: 0101000 -> There are 2 subarrays of length 1 containing only 1s. Input: str = "11111001", K=3Output: 3 Approach: The given problem can also be 4 min read Like