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.
Input: N = 32, K = 4
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
2050 can be split into 2, 0, 5, 0 where 0, 5, 0 are divisible by 5.
Input: N = 00001242, K = 3
00001242 can be split into 0, 0, 0, 0, 12, 42 where all the parts are divisible by 3.
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.
Below is the implementation of the above logic
- 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 email@example.com. 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.