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 subsequence of the form 0*1*0* in a binary string
- Longest subsequence whose sum is divisible by a given number
- Find length of longest subsequence of one string which is substring of another string
- Periodic Binary String With Minimum Period and a Given Binary String as Subsequence.
- Longest Subsequence of a String containing only Consonants
- Longest Subsequence of a String containing only vowels
- Longest subsequence with at least one character appearing in every string
- Longest Increasing Subsequence using Longest Common Subsequence Algorithm
- Longest sub string of 0's in a binary string which is repeated K times
- Largest sub-string of a binary string divisible by 2
- Decimal representation of given binary string is divisible by 5 or not
- Number of subsequences in a given binary string divisible by 2
- Number of sub-strings in a given binary string divisible by 2
- Number of sub-sequences of non-zero length of a binary string divisible by 3
- Length of longest consecutive ones by at most one swap in a Binary String
- Maximum splits in binary string such that each substring is divisible by given odd number
- Minimum swaps required to make a binary string divisible by 2^k
- Longest subsequence such that every element in the subsequence is formed by multiplying previous element with a prime
- Count maximum occurrence of subsequence in string such that indices in subsequence is in A.P.
- Longest Zig-Zag Subsequence
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.