# Split the number N by maximizing the count of subparts divisible by K

Given a numeric string N and an integer K, the task is to split digits of N into subparts such that the number of segments divisible by K is maximized.

Note: We can make any number of vertical cuts between pairs of adjacent digits.

Examples:

Input: N = 32, K = 4
Output: 1
Explanation:
32 is divisible by 4 but none if its digits are individually divisible so we don’t perform any splits.

Input: N = 2050, K = 5
Output: 3
Explanation:
2050 can be split into 2, 0, 5, 0 where 0, 5, 0 are divisible by 5.

Input: N = 00001242, K = 3
Output: 6
Explanation:
00001242 can be split into 0, 0, 0, 0, 12, 42 where all the parts are divisible by 3.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

To solve the problem mentioned above we will try to use a recursive approach.

• Check if there is a vertical partition between the current character and the next one, if no then we perform recursion again for the next index and update the substring value by concatenating present character.
• Now, if there is a vertical partition between the current character and the next character, then there exist two cases:
1. If the present subStr is divisible by X, then we add 1 because the present subStr is 1 of the possible answer, then recur for next index and update subStr as an empty string.
2. If the present subStr is not divisible by X, then we simply recur for the next index and update subStr as an empty string.
• Return a maximum of the two possible cases mentioned above.

Below is the implementation of the above logic

## C++

 `// C++ program to split the number N ` `// by maximizing the count ` `// of subparts divisible by K ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to count the subparts ` `int` `count(string N, ``int` `X, ` `          ``string subStr, ` `          ``int` `index, ``int` `n) ` `{ ` ` `  `    ``if` `(index == n) ` `        ``return` `0; ` ` `  `    ``// Total subStr till now ` `    ``string a = subStr + N[index]; ` ` `  `    ``// b marks the subString uptil now ` `    ``// is divisible by X or not, ` ` `  `    ``// If it can be divided, ` `    ``// then this substring is one ` `    ``// of the possible answer ` `    ``int` `b = 0; ` ` `  `    ``// Convert string to long long and ` `    ``// check if its divisible with X ` `    ``if` `(stoll(a) % X == 0) ` `        ``b = 1; ` ` `  `    ``// Consider there is no vertical ` `    ``// cut between this index and the ` `    ``// next one, hence take total ` `    ``// carrying total substr a. ` `    ``int` `m1 = count(N, X, a, index + 1, n); ` ` `  `    ``// 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 ` `    ``int` `m2 = b + count(N, X, ``""``, ` `                       ``index + 1, n); ` ` `  `    ``// Return max of both the cases ` `    ``return` `max(m1, m2); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``string N = ``"00001242"``; ` ` `  `    ``int` `K = 3; ` ` `  `    ``int` `l = N.length(); ` ` `  `    ``cout << count(N, K, ``""``, 0, l) ` `         ``<< endl; ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to split the number N ` `// by maximizing the count ` `// of subparts divisible by K ` `class` `GFG{ ` ` `  `// Function to count the subparts ` `static` `int` `count(String N, ``int` `X, ` `                 ``String subStr, ` `                 ``int` `index, ``int` `n) ` `{ ` `    ``if` `(index == n) ` `        ``return` `0``; ` ` `  `    ``// Total subStr till now ` `    ``String a = subStr + N.charAt(index); ` ` `  `    ``// b marks the subString uptil now ` `    ``// is divisible by X or not, ` ` `  `    ``// If it can be divided, ` `    ``// then this subString is one ` `    ``// of the possible answer ` `    ``int` `b = ``0``; ` ` `  `    ``// Convert String to long and ` `    ``// check if its divisible with X ` `    ``if` `(Long.valueOf(a) % X == ``0``) ` `        ``b = ``1``; ` ` `  `    ``// Consider there is no vertical ` `    ``// cut between this index and the ` `    ``// next one, hence take total ` `    ``// carrying total substr a. ` `    ``int` `m1 = count(N, X, a, index + ``1``, n); ` ` `  `    ``// 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 ` `    ``int` `m2 = b + count(N, X, ``""``, ` `                       ``index + ``1``, n); ` ` `  `    ``// Return max of both the cases ` `    ``return` `Math.max(m1, m2); ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``String N = ``"00001242"``; ` ` `  `    ``int` `K = ``3``; ` ` `  `    ``int` `l = N.length(); ` ` `  `    ``System.out.print(count(N, K, ``""``, ``0``, l) + ``"\n"``); ` `} ` `} ` ` `  `// This code is contributed by Rajput-Ji `

## Python3

 `# Python3 program to split the number N ` `# by maximizing the count ` `# of subparts divisible by K ` ` `  `# Function to count the subparts ` `def` `count(N, X, subStr, index, n): ` `     `  `    ``if` `(index ``=``=` `n): ` `        ``return` `0` `     `  `    ``# Total subStr till now ` `    ``a ``=` `subStr ``+` `N[index] ` `     `  `    ``# b marks the subString uptil now ` `    ``# is divisible by X or not, ` ` `  `    ``# If it can be divided, ` `    ``# then this substring is one ` `    ``# of the possible answer ` `    ``b ``=` `0` ` `  `    ``# Convert string to long long and ` `    ``# check if its divisible with X ` `    ``if` `(``int``(a) ``%` `X ``=``=` `0``): ` `        ``b ``=` `1` `     `  `    ``# Consider there is no vertical ` `    ``# cut between this index and the ` `    ``# next one, hence take total ` `    ``# carrying total substr a. ` `    ``m1 ``=` `count(N, X, a, index ``+` `1``, n) ` ` `  `    ``# 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 ` `    ``m2 ``=` `b ``+` `count(N, X, "", index ``+` `1``, n) ` ` `  `    ``# Return max of both the cases ` `    ``return` `max``(m1, m2)  ` `     `  `# Driver code ` `N ``=` `"00001242"` `K ``=` `3` ` `  `l ``=` `len``(N) ` ` `  `print``(count(N, K, "", ``0``, l)) ` ` `  `# This code is contributed by sanjoy_62  `

## C#

 `// C# program to split the number N ` `// by maximizing the count ` `// of subparts divisible by K ` `using` `System; ` `class` `GFG{ ` ` `  `// Function to count the subparts ` `static` `int` `count(String N, ``int` `X, ` `                 ``String subStr, ` `                 ``int` `index, ``int` `n) ` `{ ` `    ``if` `(index == n) ` `        ``return` `0; ` ` `  `    ``// Total subStr till now ` `    ``String a = subStr + N[index]; ` ` `  `    ``// b marks the subString uptil now ` `    ``// is divisible by X or not, ` ` `  `    ``// If it can be divided, ` `    ``// then this subString is one ` `    ``// of the possible answer ` `    ``int` `b = 0; ` ` `  `    ``// Convert String to long and ` `    ``// check if its divisible with X ` `    ``if` `(``long``. Parse(a) % X == 0) ` `        ``b = 1; ` ` `  `    ``// Consider there is no vertical ` `    ``// cut between this index and the ` `    ``// next one, hence take total ` `    ``// carrying total substr a. ` `    ``int` `m1 = count(N, X, a, index + 1, n); ` ` `  `    ``// 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 ` `    ``int` `m2 = b + count(N, X, ``""``, ` `                  ``index + 1, n); ` ` `  `    ``// Return max of both the cases ` `    ``return` `Math.Max(m1, m2); ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``String N = ``"00001242"``; ` ` `  `    ``int` `K = 3; ` ` `  `    ``int` `l = N.Length; ` ` `  `    ``Console.Write(count(N, K, ``""``, 0, l) + ``"\n"``); ` `} ` `} ` ` `  `// This code is contributed by Rajput-Ji `

Output:

```6
```

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.

Improved By : Rajput-Ji, sanjoy_62