Given a large number in string format and we are also given two numbers f and s. We need to divide the large number into two continuous parts such that the first part is divisible by f and the second part is divisible by s.
Input: num = “246904096” f = 12345 s = 1024 Output: Yes We can divide num into “24680” and “4096” which are divisible by f and s respectively. Input : num = “1234” f = 1 s = 123 Output : No We can not divide num into two parts under given constraint.
A simple solution is to generate all divisions of a given number and check if a division is divisible.
We can solve this problem by storing remainders of each prefix and suffix of the given string and if at any index, prefix reminder and suffix reminder both are zero, then we know that we can break the string at this index. The procedure is explained for the above example,
String = “246904096” All prefix reminders with 12345 are, Prefix 2 reminder with 12345, 2 prefix 24 reminder with 12345, 24 prefix 246 reminder with 12345, 246 prefix 2469 reminder with 12345, 2469 prefix 24690 reminder with 12345, 0 prefix 246904 reminder with 12345, 4 prefix 2469040 reminder with 12345, 40 prefix 24690409 reminder with 12345, 409 prefix 246904096 reminder with 12345, 4096 All suffix reminder with 1024 are, Suffix 6 reminder with 1024, 6 Suffix 96 reminder with 1024, 96 Suffix 096 reminder with 1024, 96 Suffix 4096 reminder with 1024, 0 Suffix 04096 reminder with 1024, 0 Suffix 904096 reminder with 1024, 928 Suffix 6904096 reminder with 1024, 288 Suffix 46904096 reminder with 1024, 800 Suffix 246904096 reminder with 1024, 288 Now we can see that at index 5 both reminders are 0, so the string can be broken into 24680 and 4096.
We can get (i)th suffix reminder by (i – 1)th suffix reminder as, (sr[i] = sr[i – 1] * 10 + s[i]) % f, i.e. just multiply previous reminder by 10 and add current digit and then take reminder by f.
For getting (i)th prefix reminder by (i + 1)th prefix reminder, we can do, (pr[i] = pr[i + 1] + s[i] * base) % s, i.e. add next reminder and current digit multiplied with base value which will be 1 for last digit, 10 for second last digit and so on and then we will take reminder by s overall.
Total time complexity of solution will O(N)
This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Split a number into 3 parts such that none of the parts is divisible by 3
- Divide a number into two parts
- Divide a big number into two parts that differ by k
- Divide a number into two parts such that sum of digits is maximum
- Find the number of ways to divide number into four parts such that a = c and b = d
- Count number of ways to divide a number in 4 parts
- Possible cuts of a number such that maximum parts are divisible by 3
- Divide an isosceles triangle in two parts with ratio of areas as n:m
- Minimum cuts required to divide the Circle into equal parts
- Smallest number divisible by first n numbers
- Sum of n digit numbers divisible by a given number
- Numbers that are not divisible by any number in the range [2, 10]
- Largest N digit number divisible by given three numbers
- Program to print all the numbers divisible by 3 and 5 for a given number
- Smallest n digit number divisible by given three numbers