Given K, number of digits, and d0 and d1 as the two digits to form the k-sized integer. Task is to check whether the k-sized number formed using d0 and d1 is divisible by 3 or not.
For each i, di is the sum of all preceding (more significant) digits, modulo 10 — more formally, the following formula must hold :
Input : K = 5 d0 = 3 d1 = 4 Output : NO Explanation : The whole number N is 34748 (Starting from third digit, every digit is some of preceding digits mod 10). Since 34748 is not divisible by 3, answer is NO. 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.
K may be very long so generating the entire number, calculating the sum of digits and then checking for multiple of 3 is cumbersome. The key idea behind the solution is that the digits start repeating after some time in a cycle of length 4 and then the sum of digits can be determined in O(1) step.
We know d0 and d1,
So, d2 = (d0 + d1) mod 10
d3 = (d2 + d1 + d0) mod 10 = ((d0 + d1) mod 10 + (d0 + d1)) mod 10 = 2 * (d0 + d1) mod 10
d4 = (d3 + d2 + d1 + d0) mod 10 = 4 * (d0 + d1) mod 10
d5 = (d4 + d3 + d2 + d1 + d0) mod 10 = 8 * (d0 + d1) mod 10
d6 = (d5 +…+ d1 + d0) mod 10 = 16 * (d0 + d1) mod 10 = 6 * (d0 + d1) mod 10
d7 = (d6 +…+ d1 + d0) mod 10 = 32 * (d0 + d1) mod 10 = 2 * (d0 + d1) mod 10
If we keep on getting di, we will see that the resultant is just looping through the same values. We can see that (d0 + d1) contributes 1 time(s) to d2, 2 times to d3, 4 times to d4, 8 times to d5, …, 2^(k – 2) times to dk.
But, since the powers of 2 under modulo 10 cycle from 1, 2, 4, 8, 6, 2, 4. Here, the cycle length is of 4, where d2 is not present in the cycle. Let, S = (2 * (d0 + d1)) mod 10 + (4 * (d0 + d1)) mod 10 + (8 * (d0 + d1)) mod 10 + (6 * (d0 + d1)) mod 10, this is the cycle which repeats.
So, the sum of digits = (d0 + d1 + d2) + S * ( (k – 3) / 4 ) + x. Here, the first 3 terms will be covered by d0, d1, d2 and after that the groups of 4 will be covered by S, but this formula will still has not summed some terms at the end. That is the residue that is noted by x.
Below is the implementation of above approach :
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.
- Count of N-digit numbers having digit XOR as single digit
- Minimum digits to be removed to make either all digits or alternating digits same
- Check if frequency of each digit is less than the digit
- Generate a number such that the frequency of each digit is digit times the frequency in given number
- Min steps to convert N-digit prime number into another by replacing a digit in each step
- Print all n-digit numbers whose sum of digits equals to given sum
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Largest number less than N with digit sum greater than the digit sum of N
- Minimize increment/decrement of Array elements to make each modulo K equal
- Check divisibility by 7
- Sub-string Divisibility by 3 Queries
- Sub-string Divisibility by 11 Queries
- To check divisibility of any large number by 999
- Check if a large number is divisibility by 15
- Divisibility by 12 for a large number
- Repeated Unit Divisibility
- Maximize profit when divisibility by two numbers have associated profits
- Check the divisibility of Hexadecimal numbers
- Print all n-digit numbers with absolute difference between sum of even and odd digits is 1
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.