Given a binary string str, the task is to calculate the maximum possible splits possible to make each substring divisible by a given odd number K.
Input: str = “110111001”, K = 9
The two possible substrings are “11011” and “1001”. The equivalent decimal values are 27 and 9 respectively which are divisible by 9.
Input: str = “10111001”, K = 5
The two possible substrings are “101” and “11001”. The equivalent decimal values are 5 and 25 respectively which are divisible by 5.
Approach: In order to solve this problem, we traverse from the end of the string and generate the sum of the length traversed. As soon as the sum is divisible by K, we increase the count by 1 and reset sum to 0 and traverse forward and repeat the same process. On full traversal of the string, if sum has been reset to 0, then the value of the count gives the required maximum possible splits. Otherwise, print “Not Possible” as all segments are not divisible by K.
Below code is the implementation of the above approach:
- Minimum splits in a binary string such that every substring is a power of 4 or 6.
- Maximum number of set bits count in a K-size substring of a Binary String
- Maximum number of splits of a binary number
- 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
- Maximize the numbers of splits in an Array having sum divisible by 3
- Find the maximum possible Binary Number from given string
- Lexicographical Maximum substring of string
- Minimum steps to remove substring 010 from a binary string
- Maximum length substring with highest frequency in a string
- Largest sub-string of a binary string divisible by 2
- Minimum flips required in a binary string such that all K-size substring contains 1
- Maximize count of 0s in left and 1s in right substring by splitting given 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 10 or not
- Decimal representation of given binary string is divisible by 5 or not
- Decimal representation of given binary string is divisible by 20 or not
- Number of binary strings such that there is no substring of length ≥ 3
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.