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:
- Minimum splits in a binary string such that every substring is a power of 4 or 6.
- 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
- Count number of trailing zeros in Binary representation of a number using Bitset
- Largest number dividing maximum number of elements in the array
- Check if the binary representation of a number has equal number of 0s and 1s in blocks
- Number of ways to split a binary number such that every part is divisible by 2
- Represent a number as a sum of maximum possible number of Prime Numbers
- Number of steps required to convert a binary number to one
- Number of leading zeros in binary representation of a given number
- Minimum number of Binary strings to represent a Number
- Binary representation of next greater number with same number of 1's and 0's
- Find a value whose XOR with given number is maximum
- Binary representation of a given 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 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.