Given a binary string S of length N, the task is to find the length of the longest sub-sequence in it which is divisible by 3. Leading zeros in the sub-sequences are allowed.
Input: S = “1001”
The longest sub-sequence divisible by 3 is “1001”.
1001 = 9 which is divisible by 3.
Input: S = “1011”
Naive approach: Generate all the possible sub-sequences and check if they are divisible by 3. The time complexity for this will be O((2N) * N).
Efficient approach: Dynamic programming can be used to solve this problem. Let’s look at the states of DP.
DP[i][r] will store the longest sub-sequence of the substring S[i…N-1] such that it gives a remainder of (3 – r) % 3 when divided by 3.
Let’s write the recurrence relation now.
DP[i][r] = max(1 + DP[i + 1][(r * 2 + s[i]) % 3], DP[i + 1][r])
The recurrence is derived because of the following two choices:
- 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 the current index in the sub-sequence.
Below is the implementation of the above approach:
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.
- Longest Increasing Subsequence using Longest Common Subsequence Algorithm
- Longest Subsequence from a numeric String divisible by K
- Longest subsequence such that every element in the subsequence is formed by multiplying previous element with a prime
- Longest subsequence whose sum is divisible by a given number
- Length of longest subsequence having absolute difference of all pairs divisible by K
- Longest subsequence of the form 0*1*0* in a binary string
- Periodic Binary String With Minimum Period and a Given Binary String as Subsequence.
- Find length of longest subsequence of one string which is substring of another string
- Count maximum occurrence of subsequence in string such that indices in subsequence is in A.P.
- Longest subsequence with at least one character appearing in every string
- Longest Subsequence of a String containing only vowels
- Longest Subsequence of a String containing only Consonants
- Largest sub-string of a binary string divisible by 2
- Find the equal pairs of subsequence of S and subsequence of T
- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Longest sub string of 0's in a binary string which is repeated K times
- Given a large number, check if a subsequence of digits is divisible by 8
- Longest Increasing Subsequence Size (N log N)
- Longest Increasing Subsequence | DP-3
- Longest Common Subsequence | DP-4
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.