Open In App

Maximum splits in binary string such that each substring is divisible by given odd number

Last Updated : 05 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Given binary string str, the task is to calculate the maximum possible splits possible to make each substring divisible by a given odd number K.
Examples: 
 

Input: str = “110111001”, K = 9 
Output:
Explanation: 
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 
Output:
Explanation: 
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:
 

C++




// C++ Program to split
// a given binary string
// into maximum possible
// segments divisible by
// given odd number K
 
#include <bits/stdc++.h>
using namespace std;
// Function to calculate
// maximum splits possible
void max_segments(string str, int K)
{
    int n = str.length();
    int s = 0, sum = 0, count = 0;
    for (int i = n - 1; i >= 0; i--) {
        int a = str[i] - '0';
        sum += a * pow(2, s);
 
        s++;
        if (sum != 0 && sum % K == 0) {
            count++;
            sum = 0;
            s = 0;
        }
    }
    if (sum != 0)
        cout << "-1" << endl;
    else
        cout << count << endl;
}
 
// Driver code
int main()
{
    string str = "10111001";
    int K = 5;
    max_segments(str, K);
    return 0;
}


Java




// Java code to split a given
// binary string into maximum
// possible segments divisible
// by given odd number K
import java.io.*;
import java.util.*;
 
class GFG{
     
// Function to calculate
// maximum splits possible
static void rearrange(String str, int K)
{
    int n = str.length();
    int s = 0, sum = 0, count = 0;
     
    for(int i = n - 1; i >= 0; i--)
    {
       int a = str.charAt(i) - '0';
       sum += a * Math.pow(2, s);
       s++;
        
       if (sum != 0 && sum % K == 0)
       {
           count++;
           sum = 0;
             s = 0;
       }
    }
  
    if (sum != 0)
        System.out.println("-1");   
    else
        System.out.println(count);
}
 
// Driver code
public static void main(String[] args)
{
    String str = "10111001";
    int K = 5;
     
    rearrange(str, K);
}
}
 
// This code is contributed by coder001


Python3




# Python3 program to split
# a given binary string
# into maximum possible
# segments divisible by
# given odd number K
 
# Function to calculate
# maximum splits possible
def max_segments(st, K):
 
    n = len(st)
    s, sum, count = 0, 0, 0
     
    for i in range(n - 1, -1, -1):
        a = ord(st[i]) - 48
        sum += a * pow(2, s)
        s += 1
         
        if (sum != 0 and sum % K == 0):
            count += 1
            sum = 0
            s = 0
             
    if (sum != 0):
        print("-1")
    else:
        print(count)
 
# Driver code
if __name__ == "__main__":
     
    st = "10111001"
    K = 5
    max_segments(st, K)
 
# This code is contributed by chitranayal


C#




// C# program to split a given
// binary string into maximum
// possible segments divisible by
// given odd number K
using System;
 
class GFG{
     
// Function to calculate
// maximum splits possible
static void max_segments(string str, int K)
{
    int n = str.Length;
    int s = 0;
    int sum = 0;
    int count = 0;
     
    for(int i = n - 1; i >= 0; i--)
    {
       int a = str[i] - '0';
       sum += a * (int)Math.Pow(2, s);
       s++;
        
       if (sum != 0 && sum % K == 0)
       {
           count++;
           sum = 0;
             s = 0;
       }
    }
    if (sum != 0)
    {
        Console.Write("-1");
    }
    else
    {
        Console.Write(count);
    }
}
 
// Driver code
public static void Main()
{
    string str = "10111001";
    int K = 5;
     
    max_segments(str, K);
}
}
 
// This code is contributed by sayesha   


Javascript




<script>
 
// Javascript code to split a given
// binary string into maximum
// possible segments divisible
// by given odd number K
  
    
// Function to calculate
// maximum splits possible
function rearrange(str , K)
{
    var n = str.length;
    var s = 0, sum = 0, count = 0;
     
    for(var i = n - 1; i >= 0; i--)
    {
       var a = str.charAt(i) - '0';
       sum += a * Math.pow(2, s);
       s++;
        
       if (sum != 0 && sum % K == 0)
       {
           count++;
           sum = 0;
             s = 0;
       }
    }
  
    if (sum != 0)
        document.write("-1");   
    else
        document.write(count);
}
 
// Driver code
var str = "10111001";
var K = 5;
 
rearrange(str, K);
 
// This code contributed by shikhasingrajput
 
</script>


Output: 

2

 

Time Complexity: O(n)

Auxiliary Space: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads