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.

**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:
- 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. - 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 <bits/stdc++.h> ` `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; ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**Output:**

6

## Recommended Posts:

- Maximizing Probability of one type from N containers
- Number of ways to split a binary number such that every part is divisible by 2
- Count the number of pairs (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i]
- Split a number into 3 parts such that none of the parts is divisible by 3
- Split a number as sum of K numbers which are not divisible by K
- Count ways to split a Binary String into three substrings having equal count of zeros
- Find permutation of n which is divisible by 3 but not divisible by 6
- Count of ways to split a given number into prime segments
- Count all subarrays whose sum can be split as difference of squares of two Integers
- Count of ways to split given string into two non-empty palindromes
- Count of ways to split an Array into three contiguous Subarrays having increasing Sum
- Count of ways to split N into Triplets forming a Triangle
- Count ways to split N! into two distinct co-prime factors
- Split array into two subsequences having minimum count of pairs with sum equal to X
- Count ways to split array into K non-intersecting subsets
- Count ways to split array into two subarrays with equal GCD
- Count ways to split array into two equal sum subarrays by replacing each array element to 0 once
- Count n digit numbers divisible by given number
- Count number of pairs in array having sum divisible by K | SET 2
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M

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.