Maximum splits in binary string such that each substring is divisible by given odd number | Java code to split a given binary string into maximum possible segments divisible by given odd number K ; Function to calculate maximum splits possible ; Driver code
import java . io . * ; import java . util . * ; class GFG { 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 ) ; } public static void main ( String [ ] args ) { String str = "10111001" ; int K = 5 ; rearrange ( str , K ) ; } }