Given a Large number N ( number of digits in N can be up to 105). The task is to find the cuts required of a number such that maximum parts are divisible by 3.
Input: N = 1269 Output: 3 Cut the number as 12|6|9. So, 12, 6, 9 are the three numbers which are divisible by 3. Input: N = 71 Output: 0 However, we make cuts there is no such number that is divisible by 3.
Let’s calculate the values of the array res[0…n], where res[i] is the answer for the prefix of the length i. Obviously, res:=0, since for the empty string (the prefix of the length 0) the answer is 0.
For i>0 one can find res[i] in the following way:
- Let’s look at the last digit of the prefix of length i. It has index i-1. Either it doesn’t belong to segment divisible by 3, or it belongs.
- If it doesn’t belong, it means last digit can’t be used, so res[i]=res[i-1]. If it belongs then find shortest s[j..i-1] that is divisible by 3 and try to update res[i] with the value res[j]+1.
- A number is divisible by 3, if and only if the sum of its digits is divisible by 3. So the task is to find the shortest suffix of s[0…i-1] with the sum of digits divisible by 3. If such suffix is s[j..i-1] then s[0..j-1] and s[0..i-1] have the same remainder of the sum of digits modulo 3.
- Let’s maintain remIndex[0..2]- an array of the length 3, where remIndex[r] is the length of the longest processed prefix with the sum of digits equal to r modulo 3. Use remIndex[r]= -1 if there is no such prefix. It is easy to see that j=remIndex[r] where r is the sum of digits on the ith prefix modulo 3.
- So to find the maximal j<=i-1 that substring s[j..i-1] is divisible by 3, just check that remIndex[r] not equals to -1 and use j=remIndex[r], where r is the sum of digits on the i-th prefix modulo 3.
- It means that to handle case that the last digit belongs to divisible by 3 segment, try to update res[i] with value res[remIndex[r]]+1. In other words, just do if (remIndex[r] != -1) => res[i] = max(res[i], res[remIndex[r]] + 1).
Below is the implementation of the above approach:
- Split a number into 3 parts such that none of the parts is divisible by 3
- Minimum Cuts can be made in the Chessboard such that it is not divided into 2 parts
- Maximum number of pieces in N cuts
- Minimum cuts required to divide the Circle into equal parts
- Divide number into two parts divisible by given numbers
- Divide a number into two parts such that sum of digits is maximum
- Recursively break a number in 3 parts to get maximum sum
- Break a number such that sum of maximum divisors of all parts is minimum
- Minimum and Maximum element of an array which is divisible by a given number k
- Split a string in equal parts such that all parts are palindromes
- Minimum number of cuts required to make circle segments equal sized
- Count number of ways to divide a number in 4 parts
- Break the number into three parts
- Divide a big number into two parts that differ by k
- Partition a number into two divisble parts
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.