Given a binary string **str** of length **N**, the task is to find the count of subsequences of **str** which are divisible by **2**. Leading zeros in a sub-sequence is allowed.

**Examples:**

Input:str = “101”

Output:2

“0” and “10” are the only subsequences

which are divisible by 2.

Input:str = “10010”

Output:22

**Naive approach:** A naive approach will be to generate all possible sub-sequences and check if they are divisible by 2. The time complexity for this will be O(2^{N} * N).

**Efficient approach:** It can be observed that any binary number is divisible by **2** only if it ends with a **0**. Now, the task is to just count the number of subsequences ending with **0**. So, for every index **i** such that **str[i] = ‘0’**, find the number of subsequences ending at **i**. This value is equal to **2 ^{i}** (0-based indexing). Thus, the final answer will be equal to the summation of

**2**for all

^{i}**i**such that

**str[i] = ‘0’**.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the count ` `// of the required subsequences ` `int` `countSubSeq(string str, ` `int` `len) ` `{ ` ` ` `// To store the final answer ` ` ` `int` `ans = 0; ` ` ` ` ` `// Multiplier ` ` ` `int` `mul = 1; ` ` ` ` ` `// Loop to find the answer ` ` ` `for` `(` `int` `i = 0; i < len; i++) { ` ` ` ` ` `// Condition to update the answer ` ` ` `if` `(str[i] == ` `'0'` `) ` ` ` `ans += mul; ` ` ` `// updating multiplier ` ` ` `mul *= 2; ` ` ` `} ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string str = ` `"10010"` `; ` ` ` `int` `len = str.length(); ` ` ` ` ` `cout << countSubSeq(str, len); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` `// Function to return the count ` `// of the required subsequences ` `static` `int` `countSubSeq(String str, ` `int` `len) ` `{ ` ` ` `// To store the final answer ` ` ` `int` `ans = ` `0` `; ` ` ` ` ` `// Multiplier ` ` ` `int` `mul = ` `1` `; ` ` ` ` ` `// Loop to find the answer ` ` ` `for` `(` `int` `i = ` `0` `; i < len; i++) ` ` ` `{ ` ` ` ` ` `// Condition to update the answer ` ` ` `if` `(str.charAt(i) == ` `'0'` `) ` ` ` `ans += mul; ` ` ` ` ` `// updating multiplier ` ` ` `mul *= ` `2` `; ` ` ` `} ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `String str = ` `"10010"` `; ` ` ` `int` `len = str.length(); ` ` ` ` ` `System.out.print(countSubSeq(str, len)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the count ` `# of the required subsequences ` `def` `countSubSeq(strr, lenn): ` ` ` ` ` `# To store the final answer ` ` ` `ans ` `=` `0` ` ` ` ` `# Multiplier ` ` ` `mul ` `=` `1` ` ` ` ` `# Loop to find the answer ` ` ` `for` `i ` `in` `range` `(lenn): ` ` ` ` ` `# Condition to update the answer ` ` ` `if` `(strr[i] ` `=` `=` `'0'` `): ` ` ` `ans ` `+` `=` `mul ` ` ` ` ` `# updating multiplier ` ` ` `mul ` `*` `=` `2` ` ` ` ` `return` `ans ` ` ` `# Driver code ` `strr ` `=` `"10010"` `lenn ` `=` `len` `(strr) ` ` ` `print` `(countSubSeq(strr, lenn)) ` ` ` `# This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the count ` ` ` `// of the required subsequences ` ` ` `static` `int` `countSubSeq(` `string` `str, ` `int` `len) ` ` ` `{ ` ` ` `// To store the final answer ` ` ` `int` `ans = 0; ` ` ` ` ` `// Multiplier ` ` ` `int` `mul = 1; ` ` ` ` ` `// Loop to find the answer ` ` ` `for` `(` `int` `i = 0; i < len; i++) ` ` ` `{ ` ` ` ` ` `// Condition to update the answer ` ` ` `if` `(str[i] == ` `'0'` `) ` ` ` `ans += mul; ` ` ` ` ` `// updating multiplier ` ` ` `mul *= 2; ` ` ` `} ` ` ` `return` `ans; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `static` `public` `void` `Main () ` ` ` `{ ` ` ` `string` `str = ` `"10010"` `; ` ` ` `int` `len = str.Length; ` ` ` ` ` `Console.WriteLine(countSubSeq(str, len)); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

**Output:**

22

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:

- Number of subsequences in a string divisible by n
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9 | Set 2
- Minimum number of palindromic subsequences to be removed to empty a binary string
- Construct a string that has exactly K subsequences from given string
- Subsequences of size three in an array whose sum is divisible by m
- Find the count of subsequences where each element is divisible by K
- Number of sub-strings in a given binary string divisible by 2
- Maximum splits in binary string such that each substring is divisible by given odd number
- Count subsequences in first string which are anagrams of the second string
- Largest sub-string of a binary string divisible by 2
- Generating distinct subsequences of a given string in lexicographic order
- Count of 'GFG' Subsequences in the given string
- Lexicographically smallest permutation of a string with given subsequences
- Convert given string to another by minimum replacements of subsequences by its smallest character
- Number of subsequences as "ab" in a string repeated K times
- Number of ways to partition a string into two balanced subsequences
- Minimum number of subsequences required to convert one string to another
- Minimum number of subsequences required to convert one string to another using Greedy Algorithm
- Decimal representation of given binary string is divisible by 5 or not

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.