Given a number (as string) and two integers a and b, divide the string in two non-empty parts such that the first part is divisible by a and second part is divisible by b. If string can not be divided into two non-empty parts, output “NO”, else print “YES” with the two parts.
Input : str = "123", a = 12, b = 3 Output : YES 12, 3 "12" is divisible by a and "3" is divisible by b. Input : str = "1200", a = 4, b = 3 Output : YES 12, 00 Input : str = "125", a = 12, b = 3 Output : NO
A simple solution is to one by one partition array around all points. For every partition, check if left and right of it are divisible by a and b respectively. If yes, print the left and right parts and return.
An efficient solution is to do some preprocessing and save the division modulo by ‘a’ by scanning the string from left to right and division modulo by ‘b’ from right to left.
If we know the remainder of prefix from 0 to i, when divided by a, then we compute remainder of prefix from 0 to i+1 using below formula.
lr[i+1] = (lr[i]*10 + str[i] -‘0’)%a.
Same way, modulo by b can be found by scanning from right to left. We create another rl to store remainders with b from right to left.
Once we have precomputed two remainders, we can easily find the point that partition string in two parts.
YES 12, 3
Time Complexity : O(len) where len is length of input number string.
This article is contributed by Ekta Goel. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.about the topic discussed above
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Split a number into 3 parts such that none of the parts is divisible by 3
- Partition the string in two parts such that both parts have at least k different characters
- Possible cuts of a number such that maximum parts are divisible by 3
- Divide number into two parts divisible by given numbers
- Partition N into M parts such that difference between Max and Min part is smallest
- Count ways to partition a string such that both parts have equal distinct characters
- Split a string in equal parts such that all parts are palindromes
- Count number of ways to divide a number in 4 parts
- Find the number of ways to divide number into four parts such that a = c and b = d
- Divide N into K unique parts such that gcd of those parts is maximum
- 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]
- Divide a number into two parts
- Break the number into three 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
- Sum of all second largest divisors after splitting a number into one or more parts
- Bell Numbers (Number of ways to Partition a Set)
- Count number of ways to partition a set into k subsets
- Number of ways N can be divided into four parts to construct a rectangle
- Break a number such that sum of maximum divisors of all parts is minimum