Given a binary string str of length N, the task is to find the longest sub-string divisible by 2. If no such sub-string exists then print -1.
Input: str = “11100011”
Largest sub-string divisible by 2 is “111000”.
Input: str = “1111”
There is no sub-string of the given string
which is divisible by 2.
Naive approach: A naive approach will be to generate all such sub-strings and check if they are divisible by 2. The time complexity of this approach will be O(N3).
Better approach: A straight forward approach will be to remove characters from the end of the string while the last character is 1. The moment a 0 is encountered, the current string will be divisible by 2 as it ends at a 0. The time complexity of this approach will be O(N).
Below is the implementation of the above approach:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Maximum splits in binary string such that each substring is divisible by given odd number
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Minimum steps to remove substring 010 from a binary string
- Minimum splits in a binary string such that every substring is a power of 4 or 6.
- Maximize count of 0s in left and 1s in right substring by splitting given Binary string
- Minimum flips required in a binary string such that all K-size substring contains 1
- Maximum number of set bits count in a K-size substring of a Binary String
- Count substring of Binary string such that each character belongs to a palindrome of size greater than 1
- Longest sub-sequence of a binary string divisible by 3
- 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
- Minimum swaps required to make a binary string divisible by 2^k
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
- Smallest non-zero substring which has any permutation divisible by 2^K
- Largest substring with same Characters
- Largest substring where all characters appear at least K times | Set 2
- Largest substring of str2 which is a prefix of str1
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.
Improved By : AnkitRai01