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)
- Sum of Digits in a^n till a single digit
- Count of n digit numbers whose sum of digits equals to given sum
- Count numbers formed by given two digit with sum having given digits
- Finding sum of digits of a number until sum becomes single digit
- Print all n-digit numbers whose sum of digits equals to given sum
- Count of N-digit numbers with all distinct digits
- N digit numbers divisible by 5 formed from the M digits
- Print all n-digit numbers with absolute difference between sum of even and odd digits is 1
- Count positive integers with 0 as a digit and maximum 'd' digits
- Find last five digits of a given five digit number raised to power five
- Generate k digit numbers with digits in strictly increasing order
- Numbers less than N that are perfect cubes and the sum of their digits reduced to a single digit is 1
- Count total number of N digit numbers such that the difference between sum of even and odd digits is 1
- Maximum of sum and product of digits until number is reduced to a single digit
- Reduce number to a single digit by subtracting adjacent digits repeatedly
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.