Split the number N by maximizing the count of subparts divisible by K | Java program to split the number N by maximizing the count of subparts divisible by K ; Function to count the subparts ; Total subStr till now ; If it can be divided , then this subString is one of the possible answer ; Convert String to long and check if its divisible with X ; Consider there is no vertical cut between this index and the next one , hence take total carrying total substr a . ; If there is vertical cut between this index and next one , then we start again with subStr as " " and add b for the count of subStr upto now ; Return max of both the cases ; Driver code
class GFG { static int count ( String N , int X , String subStr , int index , int n ) { if ( index == n ) return 0 ; String a = subStr + N . charAt ( index ) ; int b = 0 ; if ( Long . valueOf ( a ) % X == 0 ) b = 1 ; int m1 = count ( N , X , a , index + 1 , n ) ; int m2 = b + count ( N , X , " " , index + 1 , n ) ; return Math . max ( m1 , m2 ) ; } public static void main ( String [ ] args ) { String N = "00001242" ; int K = 3 ; int l = N . length ( ) ; System . out . print ( count ( N , K , " " , 0 , l ) + "NEW_LINE"); } }