Check whether a very large number of the given form is a multiple of 3.
Last Updated :
21 Jun, 2022
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.
Constraints:
2 ≤K ≤1012
1 ≤d0 ≤9
0 ≤d1 ≤9
Examples:
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
Similarly,
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 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.
For e.g. 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 :
C++
#include <bits/stdc++.h>
using namespace std;
bool multipleOfThree( int K, int dig0, int dig1)
{
long long int sum = 0;
int temp = (dig0 + dig1) % 10;
sum = dig0 + dig1;
if (K == 2) {
if (sum % 3 == 0)
return true ;
else
return false ;
}
sum += temp;
long long int numberofGroups = (K - 3) / 4;
int remNumberofDigits = (K - 3) % 4;
if (temp == 5 || temp == 0)
sum += (numberofGroups * 0);
else
sum += (numberofGroups * 20);
for ( int i = 0; i < remNumberofDigits; i++) {
temp = (2 * temp) % 10;
sum += temp;
}
if (sum % 3 == 0)
return true ;
else
return false ;
}
int main()
{
int K = 5, dig0 = 3, dig1 = 4;
if (multipleOfThree(K, dig0, dig1))
cout << "YES" << endl;
else
cout << "NO" << endl;
K = 10;
dig0 = 3;
dig1 = 2;
if (multipleOfThree(K, dig0, dig1))
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
|
Java
import java.io.*;
public class GFG {
static boolean multipleOfThree( int K, int dig0,
int dig1)
{
long sum = 0 ;
int temp = (dig0 + dig1) % 10 ;
sum = dig0 + dig1;
if (K == 2 ) {
if (sum % 3 == 0 )
return true ;
else
return false ;
}
sum += temp;
long numberofGroups = (K - 3 ) / 4 ;
int remNumberofDigits = (K - 3 ) % 4 ;
sum += (numberofGroups * 20 );
for ( int i = 0 ; i < remNumberofDigits; i++) {
temp = ( 2 * temp) % 10 ;
sum += temp;
}
if (sum % 3 == 0 )
return true ;
else
return false ;
}
static public void main(String[] args)
{
int K = 5 , dig0 = 3 , dig1 = 4 ;
if (multipleOfThree(K, dig0, dig1))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python 3
def multipleOfThree(K, dig0, dig1):
sum = 0
temp = (dig0 + dig1) % 10
sum = dig0 + dig1
if (K = = 2 ):
if ( sum % 3 = = 0 ):
return True
else :
return False
sum + = temp
numberofGroups = (K - 3 ) / / 4
remNumberofDigits = (K - 3 ) % 4
sum + = (numberofGroups * 20 )
for i in range (remNumberofDigits):
temp = ( 2 * temp) % 10
sum + = temp
if ( sum % 3 = = 0 ):
return True
else :
return False
if __name__ = = "__main__" :
K = 5
dig0 = 3
dig1 = 4
if (multipleOfThree(K, dig0, dig1)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG {
static bool multipleOfThree( int K, int dig0, int dig1)
{
long sum = 0;
int temp = (dig0 + dig1) % 10;
sum = dig0 + dig1;
if (K == 2) {
if (sum % 3 == 0)
return true ;
else
return false ;
}
sum += temp;
long numberofGroups = (K - 3) / 4;
int remNumberofDigits = (K - 3) % 4;
sum += (numberofGroups * 20);
for ( int i = 0; i < remNumberofDigits; i++) {
temp = (2 * temp) % 10;
sum += temp;
}
if (sum % 3 == 0)
return true ;
else
return false ;
}
static public void Main(String[] args)
{
int K = 5, dig0 = 3, dig1 = 4;
if (multipleOfThree(K, dig0, dig1))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
PHP
<?php
function multipleOfThree( $K , $dig0 , $dig1 )
{
$sum = 0;
$temp = ( $dig0 + $dig1 ) % 10;
$sum = $dig0 + $dig1 ;
if ( $K == 2)
if ( $sum % 3 == 0)
return true;
else
return false;
$sum += $temp ;
$numberofGroups = (int)(( $K - 3) / 4);
$remNumberofDigits = ( $K - 3) % 4;
$sum += ( $numberofGroups * 20);
for ( $i = 0; $i < $remNumberofDigits ; $i ++)
{
$temp = (2 * $temp ) % 10;
$sum += $temp ;
}
if ( $sum % 3 == 0)
return true;
else
return false;
}
$K = 5;
$dig0 = 3;
$dig1 = 4;
if (multipleOfThree( $K , $dig0 , $dig1 ))
print ( "Yes" );
else
print ( "No" );
?>
|
Javascript
<script>
function multipleOfThree(K, dig0, dig1)
{
let sum = 0;
let temp = (dig0 + dig1) % 10;
sum = dig0 + dig1;
if (K == 2) {
if (sum % 3 == 0)
return true ;
else
return false ;
}
sum += temp;
let numberofGroups = parseInt((K - 3) / 4);
let remNumberofDigits = (K - 3) % 4;
if (temp == 5 || temp == 0)
sum += (numberofGroups * 0);
else
sum += (numberofGroups * 20);
for (let i = 0; i < remNumberofDigits; i++) {
temp = (2 * temp) % 10;
sum += temp;
}
if (sum % 3 == 0)
return true ;
else
return false ;
}
let K = 5, dig0 = 3, dig1 = 4;
if (multipleOfThree(K, dig0, dig1))
document.write( "YES<br>" );
else
document.write( "NO<br>" );
K = 10;
dig0 = 3;
dig1 = 2;
if (multipleOfThree(K, dig0, dig1))
document.write( "YES<br>" );
else
document.write( "NO<br>" );
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...