Skip to content
Related Articles

Related Articles

Improve Article

Check whether a very large number of the given form is a multiple of 3.

  • Difficulty Level : Medium
  • Last Updated : 08 Apr, 2021

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 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++




// CPP Program to determine if
// number N of given form is
// divisible by 3 or not
#include <bits/stdc++.h>
using namespace std;
 
// function returns true if number N is
// divisible by 3 otherwise false,
// dig0 - most significant digit
// dig1 - 2nd most significant digit
// K - number of digits
bool multipleOfThree(int K, int dig0, int dig1)
{
    // sum of digits
    long long int sum = 0;
 
    // store the sum of first two digits
    // modulo 10 in a temporary variable
    int temp = (dig0 + dig1) % 10;
 
    sum = dig0 + dig1;
 
    // if the number N is a two digit number
    if (K == 2) {
        if (sum % 3 == 0)
            return true;
        else
            return false;
    }
 
    // add temp to sum to get the sum
    // of first three digits which are
    // not a part of cycle
    sum += temp;
 
    // get the number of groups in cycle
    long long int numberofGroups = (K - 3) / 4;
 
    // get the remaining number of digits
    int remNumberofDigits = (K - 3) % 4;
 
    // if temp = 5 or temp = 0 then sum of each group will
    // be 0
    if (temp == 5 || temp == 0)
        sum += (numberofGroups * 0);
 
    else
        // add sum of 20 for each group (2, 4, 8, 6)
        sum += (numberofGroups * 20);
 
    // find the remaining sum of remaining digits
    for (int i = 0; i < remNumberofDigits; i++) {
        temp = (2 * temp) % 10;
        sum += temp;
    }
 
    // check if it is divisible by 3 or not
    if (sum % 3 == 0)
        return true;
    else
        return false;
}
 
// Driver Code
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




// Java Program to determine if
// number N of given form is
// divisible by 3 or not
import java.io.*;
 
public class GFG {
 
    // function returns true if number N is
    // divisible by 3 otherwise false,
    // dig0 - most significant digit
    // dig1 - 2nd most significant digit
    // K - number of digits
    static boolean multipleOfThree(int K, int dig0,
                                   int dig1)
    {
 
        // sum of digits
        long sum = 0;
 
        // store the sum of first two digits
        // modulo 10 in a temporary variable
        int temp = (dig0 + dig1) % 10;
 
        sum = dig0 + dig1;
 
        // if the number N is a two digit number
        if (K == 2) {
            if (sum % 3 == 0)
                return true;
            else
                return false;
        }
 
        // add temp to sum to get the sum
        // of first three digits which are
        // not a part of cycle
        sum += temp;
 
        // get the number of groups in cycle
        long numberofGroups = (K - 3) / 4;
 
        // get the remaining number of digits
        int remNumberofDigits = (K - 3) % 4;
 
        // add sum of 20 for each group (2, 4, 8, 6)
        sum += (numberofGroups * 20);
 
        // find the remaining sum of
        // remaining digits
        for (int i = 0; i < remNumberofDigits; i++) {
            temp = (2 * temp) % 10;
            sum += temp;
        }
 
        // check if it is divisible by 3 or not
        if (sum % 3 == 0)
            return true;
        else
            return false;
    }
 
    // Driver Code
    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");
    }
}
 
// This code is contributed by vt_m.

Python 3




# Python 3 Program to determine if
# number N of given form is
# divisible by 3 or not
 
# function returns true if number N
# is divisible by 3 otherwise false,
# dig0 - most significant digit
# dig1 - 2nd most significant digit
# K - number of digits
 
 
def multipleOfThree(K, dig0, dig1):
 
    # sum of digits
    sum = 0
 
    # store the sum of first two digits
    # modulo 10 in a temporary variable
    temp = (dig0 + dig1) % 10
 
    sum = dig0 + dig1
 
    # if the number N is a
    # two digit number
    if (K == 2):
        if (sum % 3 == 0):
            return True
        else:
            return False
 
    # add temp to sum to get the sum
    # of first three digits which are
    # not a part of cycle
    sum += temp
 
    # get the number of groups in cycle
    numberofGroups = (K - 3) // 4
 
    # get the remaining number of digits
    remNumberofDigits = (K - 3) % 4
 
    # add sum of 20 for each
    # group (2, 4, 8, 6)
    sum += (numberofGroups * 20)
 
    # find the remaining sum of
    # remaining digits
    for i in range(remNumberofDigits):
        temp = (2 * temp) % 10
        sum += temp
 
    # check if it is divisible
    # by 3 or not
    if (sum % 3 == 0):
        return True
    else:
        return False
 
 
# Driver Code
if __name__ == "__main__":
    K = 5
    dig0 = 3
    dig1 = 4
    if (multipleOfThree(K, dig0, dig1)):
        print("Yes")
    else:
        print("No")
 
# This code is contributed by ChitraNayal

C#




// C# Program to determine if
// number N of given form is
// divisible by 3 or not
using System;
 
class GFG {
 
    // function returns true if number N is
    // divisible by 3 otherwise false,
    // dig0 - most significant digit
    // dig1 - 2nd most significant digit
    // K - number of digits
    static bool multipleOfThree(int K, int dig0, int dig1)
    {
        // sum of digits
        long sum = 0;
 
        // store the sum of first two digits
        // modulo 10 in a temporary variable
        int temp = (dig0 + dig1) % 10;
 
        sum = dig0 + dig1;
 
        // if the number N is
        // a two digit number
        if (K == 2) {
            if (sum % 3 == 0)
                return true;
            else
                return false;
        }
 
        // add temp to sum to get the sum
        // of first three digits which are
        // not a part of cycle
        sum += temp;
 
        // get the number of groups in cycle
        long numberofGroups = (K - 3) / 4;
 
        // get the remaining number of digits
        int remNumberofDigits = (K - 3) % 4;
 
        // add sum of 20 for each group (2, 4, 8, 6)
        sum += (numberofGroups * 20);
 
        // find the remaining sum of
        // remaining digits
        for (int i = 0; i < remNumberofDigits; i++) {
            temp = (2 * temp) % 10;
            sum += temp;
        }
 
        // check if it is divisible by 3 or not
        if (sum % 3 == 0)
            return true;
        else
            return false;
    }
 
    // Driver Code
    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");
    }
}
 
// This code is contributed by vt_m.

PHP




<?php
// PHP Program to determine if number N
// of given form is divisible by 3 or not
 
// function returns true if number N
// is divisible by 3 otherwise false,
// dig0 - most significant digit
// dig1 - 2nd most significant digit
// K - number of digits
function multipleOfThree($K, $dig0, $dig1)
{
    // sum of digits
    $sum = 0;
 
    // store the sum of first two digits
    // modulo 10 in a temporary variable
    $temp = ($dig0 + $dig1) % 10;
 
    $sum = $dig0 + $dig1;
 
    // if the number N is a
    // two digit number
    if ($K == 2)
        if ($sum % 3 == 0)
            return true;
        else
            return false;
 
    // add temp to sum to get the sum
    // of first three digits which are
    // not a part of cycle
    $sum += $temp;
 
    // get the number of groups in cycle
    $numberofGroups = (int)(($K - 3) / 4);
 
    // get the remaining number of digits
    $remNumberofDigits = ($K - 3) % 4;
 
    // add sum of 20 for each
    // group (2, 4, 8, 6)
    $sum += ($numberofGroups * 20);
 
    // find the remaining sum of
    // remaining digits
    for ($i = 0; $i < $remNumberofDigits; $i++)
    {
        $temp = (2 * $temp) % 10;
        $sum += $temp;
    }
 
    // check if it is divisible
    // by 3 or not
    if ($sum % 3 == 0)
        return true;
    else
        return false;
}
 
// Driver Code
$K = 5;
$dig0 = 3;
$dig1 = 4;
if (multipleOfThree($K, $dig0, $dig1))
    print("Yes");
else
    print("No");
 
// This code is contributed by mits
?>

Javascript




<script>
 
// JavaScript Program to determine if
// number N of given form is
// divisible by 3 or not
 
// function returns true if number N is
// divisible by 3 otherwise false,
// dig0 - most significant digit
// dig1 - 2nd most significant digit
// K - number of digits
function multipleOfThree(K, dig0, dig1)
{
    // sum of digits
    let sum = 0;
 
    // store the sum of first two digits
    // modulo 10 in a temporary variable
    let temp = (dig0 + dig1) % 10;
 
    sum = dig0 + dig1;
 
    // if the number N is a two digit number
    if (K == 2) {
        if (sum % 3 == 0)
            return true;
        else
            return false;
    }
 
    // add temp to sum to get the sum
    // of first three digits which are
    // not a part of cycle
    sum += temp;
 
    // get the number of groups in cycle
    let numberofGroups = parseInt((K - 3) / 4);
 
    // get the remaining number of digits
    let remNumberofDigits = (K - 3) % 4;
 
    // if temp = 5 or temp = 0 then sum of each group will
    // be 0
    if (temp == 5 || temp == 0)
        sum += (numberofGroups * 0);
 
    else
        // add sum of 20 for each group (2, 4, 8, 6)
        sum += (numberofGroups * 20);
 
    // find the remaining sum of remaining digits
    for (let i = 0; i < remNumberofDigits; i++) {
        temp = (2 * temp) % 10;
        sum += temp;
    }
 
    // check if it is divisible by 3 or not
    if (sum % 3 == 0)
        return true;
    else
        return false;
}
 
// Driver Code
    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>
Output
NO
NO

Time Complexity: O(1)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up