Given a binary string S, the task is to find the maximum number of parts that you can split it into such that every part is divisible by 2. If the string can’t be split satisfying the given conditions then print -1.
Input: S = “100”
The splits are as follows:
“10” ans “0”.
Input: S = “110”
Approach: This problem can be solved greedily, start from the left end and put a cut at an index j such that j is the smallest index for which sub-string upto j is divisible by 2. Now, continue this step with the rest of the left-over string. It is also known that any binary number ending with a 0 is divisible by 2. Thus, put a cut after each and every zero and the answer will be equal to the number of zeros in the string. The only case where the answer is not possible is when the given string is odd i.e. no matter how cuts are made on the string, the last split part will always be odd.
Below is the implementation of the above approach:
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.
- Maximum splits in binary string such that each substring is divisible by given odd number
- Minimum splits in a binary string such that every substring is a power of 4 or 6.
- Minimum splits required to convert a number into prime segments
- Maximize the numbers of splits in an Array having sum divisible by 3
- Minimize splits to generate monotonous Substrings from given String
- Maximum XOR value of maximum and second maximum element among all possible subarrays
- Remove one bit from a binary number to get maximum value
- Find the maximum possible Binary Number from given string
- Maximum number of consecutive 1's in binary representation of all the array elements
- Maximum number of set bits count in a K-size substring of a Binary String
- Gray to Binary and Binary to Gray conversion
- Meta Binary Search | One-Sided Binary Search
- Find the occurrence of the given binary pattern in the binary representation of the array elements
- Periodic Binary String With Minimum Period and a Given Binary String as Subsequence.
- Check if binary representations of 0 to N are present as substrings in given binary string
- Maximum 0's between two immediate 1's in binary representation
- Maximum consecutive one’s (or zeros) in a binary array
- Maximum length of consecutive 1's in a binary string in Python using Map function
- Maximum difference of zeros and ones in binary string
- Maximum difference of zeros and ones in binary string | Set 2 (O(n) time)
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.