Consider a very long K-digit number N with digits d0, d1, …, dK-1 (in decimal notation; d0 is the most significant and dK-1 the least significant digit). This number is so large that it can’t be given or written down explicitly; instead, only its starting digits are given and a way to construct the remainder of the number.
Specifically, you are given d0 and d1; for each i ≥2, di is the sum of all preceding (more significant) digits, modulo 10, more formally –
Determine if N is a multiple of 3.
2 ≤K ≤1012
1 ≤d0 ≤9
0 ≤d1 ≤9
Input : K = 13, d0 = 8, d1 = 1 Output : YES
Explanation: The whole number N is 8198624862486, which is divisible by 3,
so the answer is YES.
Input : K = 5, d0 = 3, d1 = 4 Output : NO
Explanation: The whole number N is 34748, which is not divisible by 3,
so the answer is NO.
Method 1 (Brute Force)
We can apply the brute force method to calculate the whole number N by using the condition given for constructing the number iteratively (sum of preceding numbers modulo 10) and check whether the number is divisible by 3 or not. But since the number of digits (K) can be as large as 1012, we can’t store it as an integer since it will be very larger than the maximum range of ‘long long int’. Hence below is an efficient method to determine if N is a multiple of 3.
Method 2 (Efficient)
The key idea behind the solution is the fact that the digits start to repeat after some time in a cycle of length 4. Firstly, we will find the sum of all the digits and then determine if it is divisible by 3 or NOT.
We know d0 and d1.
d2 = ( d0 + d1 ) % 10
d3 = ( d2 + d1 + d0 ) % 10 = (( d0 + d1) % 10 + d0 + d1) % 10 = 2 * ( d0 + d1 ) % 10
d4 = ( d3 + d2 + d1 + d0 ) % 10 = 4 * ( d0 + d1 ) % 10
d5 = ( d4 + d3 + d2 + d1 + d0 ) % 10 = 8 * ( d0 + d1 ) % 10
d6 = ( d5 + … + d1 + d0 ) % 10 = 16 * (d0 + d3) % 10 = 6 * ( d0 + d1 ) % 10
d7 = ( d6 + … + d1 + d0 ) % 10 = 12 * ( d0 + d1 ) % 10 = 2 * ( d0 + d1 ) % 10
If we keep on finding on di, we will see that that the resultant is just looping around the same values (2, 4, 8, 6).
Here the cycle length is 4 and d2 is not present in the cycle. Hence after d2 the cycle starts forming in length of 4 starting from any value in (2, 4, 8, 6) but in the same order giving a sum of S = 2 + 4 + 8 + 6 = 20 for consecutive four digits. Thus, the total sum of digits for the whole number is = d0 + d1 + d2 + S*(k – 3)/4 + x, where first three terms will be covered by d0, d1, d2
and after that groups of 4 will be covered by S. But since (k – 3) may be not a multiple of 4, some remaining digits will be left which is covered by x which can be calculated by running a loop as those number of terms will be less than 4.
When K = 13,
sum of digits = d0 + d1 + d2 + S * (13 – 3) / 4 + x = d0 + d1 + d2 + S * 2 + x,
where first S will have d3, d4, d5, d6 and second S will have d7, d8, d9, d10 and
x = d11 + d12
- d11 = 2 * ( d0 + d1 ) % 10
- d12 = 4 * ( d0 + d1 ) % 10
Below is the implementation of above idea :
Time Complexity: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.