Given a binary string **S** of length **N**, the task is to find the number of sub-sequences of non-zero length which are divisible by **3**. Leading zeros in the sub-sequences are allowed.

**Examples:**

Input:S = “1001”

Output:5

“11”, “1001”, “0”, “0” and “00” are

the only subsequences divisible by 3.

Input:S = “1”

Output:0

**Naive approach:** Generate all the possible sub-sequences and check if they are divisible by 3. Time complexity for this will be O((2^{N}) * N).

**Better approach:** Dynamic programming can be used to solve this problem. Let’s look at the states of the DP.

**DP[i][r]** will store the number of sub-sequences of the substring **S[i…N-1]** such that they give a remainder of **(3 – r) % 3** when divided by **3**.

Let’s write the recurrence relation now.

DP[i][r] = DP[i + 1][(r * 2 + s[i]) % 3] + DP[i + 1][r]

The recurrence is derived because of the two choices below:

- Include the current index
**i**in the sub-sequence. Thus, the**r**will be updated as**r = (r * 2 + s[i]) % 3**. - Don’t include current index in the sub-sequence.

Below is the implementation of the above approach:

## C++

`// C++ implementation of th approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `#define N 100 ` ` ` `int` `dp[N][3]; ` `bool` `v[N][3]; ` ` ` `// Function to return the number of ` `// sub-sequences divisible by 3 ` `int` `findCnt(string& s, ` `int` `i, ` `int` `r) ` `{ ` ` ` `// Base-cases ` ` ` `if` `(i == s.size()) { ` ` ` `if` `(r == 0) ` ` ` `return` `1; ` ` ` `else` ` ` `return` `0; ` ` ` `} ` ` ` ` ` `// If the state has been solved ` ` ` `// before then return its value ` ` ` `if` `(v[i][r]) ` ` ` `return` `dp[i][r]; ` ` ` ` ` `// Marking the state as solved ` ` ` `v[i][r] = 1; ` ` ` ` ` `// Recurrence relation ` ` ` `dp[i][r] ` ` ` `= findCnt(s, i + 1, (r * 2 + (s[i] - ` `'0'` `)) % 3) ` ` ` `+ findCnt(s, i + 1, r); ` ` ` ` ` `return` `dp[i][r]; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string s = ` `"11"` `; ` ` ` ` ` `cout << (findCnt(s, 0, 0) - 1); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of th approach ` `class` `GFG ` `{ ` ` ` ` ` `static` `final` `int` `N = ` `100` `; ` ` ` ` ` `static` `int` `dp[][] = ` `new` `int` `[N][` `3` `]; ` ` ` `static` `int` `v[][] = ` `new` `int` `[N][` `3` `]; ` ` ` ` ` `// Function to return the number of ` ` ` `// sub-sequences divisible by 3 ` ` ` `static` `int` `findCnt(String s, ` `int` `i, ` `int` `r) ` ` ` `{ ` ` ` `// Base-cases ` ` ` `if` `(i == s.length()) ` ` ` `{ ` ` ` `if` `(r == ` `0` `) ` ` ` `return` `1` `; ` ` ` `else` ` ` `return` `0` `; ` ` ` `} ` ` ` ` ` `// If the state has been solved ` ` ` `// before then return its value ` ` ` `if` `(v[i][r] == ` `1` `) ` ` ` `return` `dp[i][r]; ` ` ` ` ` `// Marking the state as solved ` ` ` `v[i][r] = ` `1` `; ` ` ` ` ` `// Recurrence relation ` ` ` `dp[i][r] = findCnt(s, i + ` `1` `, (r * ` `2` `+ (s.charAt(i) - ` `'0'` `)) % ` `3` `) ` ` ` `+ findCnt(s, i + ` `1` `, r); ` ` ` ` ` `return` `dp[i][r]; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `String s = ` `"11"` `; ` ` ` ` ` `System.out.print(findCnt(s, ` `0` `, ` `0` `) - ` `1` `); ` ` ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of th approach ` `import` `numpy as np ` `N ` `=` `100` ` ` `dp ` `=` `np.zeros((N, ` `3` `)); ` `v ` `=` `np.zeros((N, ` `3` `)); ` ` ` `# Function to return the number of ` `# sub-sequences divisible by 3 ` `def` `findCnt(s, i, r) : ` ` ` ` ` `# Base-cases ` ` ` `if` `(i ` `=` `=` `len` `(s)) : ` ` ` ` ` `if` `(r ` `=` `=` `0` `) : ` ` ` `return` `1` `; ` ` ` `else` `: ` ` ` `return` `0` `; ` ` ` ` ` `# If the state has been solved ` ` ` `# before then return its value ` ` ` `if` `(v[i][r]) : ` ` ` `return` `dp[i][r]; ` ` ` ` ` `# Marking the state as solved ` ` ` `v[i][r] ` `=` `1` `; ` ` ` ` ` `# Recurrence relation ` ` ` `dp[i][r] ` `=` `findCnt(s, i ` `+` `1` `, (r ` `*` `2` `+` ` ` `(` `ord` `(s[i]) ` `-` `ord` `(` `'0'` `))) ` `%` `3` `) ` `+` `\ ` ` ` `findCnt(s, i ` `+` `1` `, r); ` ` ` ` ` `return` `dp[i][r]; ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `s ` `=` `"11"` `; ` ` ` ` ` `print` `(findCnt(s, ` `0` `, ` `0` `) ` `-` `1` `); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of th approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `static` `readonly` `int` `N = 100; ` ` ` ` ` `static` `int` `[,]dp = ` `new` `int` `[N, 3]; ` ` ` `static` `int` `[,]v = ` `new` `int` `[N, 3]; ` ` ` ` ` `// Function to return the number of ` ` ` `// sub-sequences divisible by 3 ` ` ` `static` `int` `findCnt(String s, ` `int` `i, ` `int` `r) ` ` ` `{ ` ` ` `// Base-cases ` ` ` `if` `(i == s.Length) ` ` ` `{ ` ` ` `if` `(r == 0) ` ` ` `return` `1; ` ` ` `else` ` ` `return` `0; ` ` ` `} ` ` ` ` ` `// If the state has been solved ` ` ` `// before then return its value ` ` ` `if` `(v[i, r] == 1) ` ` ` `return` `dp[i, r]; ` ` ` ` ` `// Marking the state as solved ` ` ` `v[i, r] = 1; ` ` ` ` ` `// Recurrence relation ` ` ` `dp[i, r] = findCnt(s, i + 1, (r * 2 + (s[i] - ` `'0'` `)) % 3) ` ` ` `+ findCnt(s, i + 1, r); ` ` ` ` ` `return` `dp[i, r]; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main(String[] args) ` ` ` `{ ` ` ` `String s = ` `"11"` `; ` ` ` ` ` `Console.Write(findCnt(s, 0, 0) - 1); ` ` ` ` ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

1

**Time Complexity:** O(n)

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 given binary string divisible by 2
- Number of subsequences in a string divisible by n
- Minimum number of palindromic subsequences to be removed to empty a binary string
- Number of palindromic subsequences of length k where k <= 3
- Number of subsequences of maximum length K containing no repeated elements
- 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
- Count subsequences in first string which are anagrams of the second string
- Construct a string that has exactly K subsequences from given string
- Largest sub-string of a binary string divisible by 2
- Find two equal subsequences of maximum length with at least one different index
- Count of subsequences of length atmost K containing distinct prime elements
- Count of subsequences of length 4 in form (x, x, x+1, x+1) | Set 2
- Number of substrings with length divisible by the number of 1's in it
- 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
- 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

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.