Given an integer **N**, the task is to print the number of binary strings of length N which at least one ‘1’.

**Examples:**

Input:2Output:3Explanation:

“01”, “10” and “11” are the possible strings

Input:3Output:7Expalnation:

“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.

Since2strings are possible of length N, the required answer is^{N}2.^{N}– 1

Follow the steps below to solve the problem:

- Initialize X = 1.
- Compute upto
**2**by performing bitwise left shift on X, N-1 times.^{N} - Finally, print X – 1 as the required answer.

Below is the implementation of our approach:

`// 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; `
`} ` |

*chevron_right*

*filter_none*

`// 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 ` |

*chevron_right*

*filter_none*

`# 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 ` |

*chevron_right*

*filter_none*

`// 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 ` |

*chevron_right*

*filter_none*

**Output:**

3

**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.

## Recommended Posts:

- Count of N size strings consisting of at least one vowel and one consonant
- Count binary strings of length same as given string after removal of substrings "01" and "00" that consists of at least one '1'
- Maximum count of sub-strings of length K consisting of same characters
- Count N-length strings consisting only of vowels sorted lexicographically
- Sub-strings that start and end with one character and have at least one other
- Count of strings that become equal to one of the two strings after one removal
- 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
- Find the number of binary strings of length N with at least 3 consecutive 1s
- Find two equal subsequences of maximum length with at least one different index
- Minimize length of Substrings containing at least one common Character
- Convert a number of length N such that it contains any one digit at least 'K' times
- Count of same length Strings that exists lexicographically in between two given Strings
- 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 ways to increase LCS length of two strings by one
- Length of the smallest sub-string consisting of maximum distinct characters
- Number of bitonic arrays of length n and consisting of elements from 1 to n
- Length of largest subsequence consisting of a pair of alternating digits
- Count pairs in an array which have at least one digit common
- Count pairs in an array such that at least one element is prime

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.