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


The problem statement includes checking whether a K-sized very long positive integer is the multiple of 3 or not where each ith digit in the K-sized number where i>1 will be the sum of all the prefix digits modulo 10 from the left.

We will be given two integers a0 and a1, where 1<=a0<=9 and 0<=a1<=9, and K which will be the number of digits in the number. The a0 and a1 are the first two digits of the number from the left. We need to calculate the K-sized number by calculating the sum of all the prefix digits modulo 10 and then check if it is multiple of 3 or not. If it is the multiple of 3, print yes in the output else we need to print no.

The modulo 10 is taken for the sum of all the prefix digits to make sure of the sum with more than one digits. If the sum is of two or more digits, we just need to take the last digit of the sum that is the one digit of the number.

Let’s understand the problem statement with the below examples which will help us to better understand the problem.

Example

INPUT : a0=2, a1=3, K=5
OUTPUT : NO

Explanation − The first two digits given as input are 2 and 3 respectively from the left of the number. We need to check for a number with 5 digits as K=5 is given. The 5 digit number formed where every digit is the sum of all the prefix digits modulo 10 is 23500. The digit a2=a1+a0=5, a3=a0+a1+a2=10 modulo 10=0. Similarly the last digit is also 0 making the number to be 23500 which is not the multiple of 3. Hence the output is NO.

INPUT : a0=1, a1=5, K=6
OUTPUT : NO

Explanation − The first two digits given are 1 and 5 and we need to find the 6 digit number by calculating the sum of all prefix digits from left modulo 10. The 6-digit number formed would be 156248 which is not the multiple of 3 hence the output will be NO.

The sum of a0 and a1 ia 6 making the number 156.

The sum of all the digits is 12 modulo 10 is 2 making the number 1562.

The sum of the digits now is 14 modulo 10 is 4 making the number 15624.

The sum of prefix digits is 18 modulo 10 is 8 making the number 156248.

Since the number of digits in the number formed is 6 which is equal to K, we will check if it is multiple of 3 or not.

Let’s understand the algorithm to find the number by calculating the sum of all prefix digits from the left and check if it is divisible by 3.

Algorithm

In this problem, for larger values of K it may take a long time to calculate the entire number and then check if it is divisible by 3 or not to know if the number is multiple of 3 or not.

The algorithm to solve the problem states that the digits when calculated as sum of all the prefix digits modulo 10, they start repeating after a particular length. In this case, the digits repeat in the cycle of 4. The first two digits will be given in the input using which we can find the third digit. After the third digit, the rest digits repeat after every four digits. So we can simply calculate the K-sized integer using this logic to check if the number is multiple of 3 or not.

Let’s understand the algorithm with the example.

Suppose we are given a0=1 and a1=3 and K=11. The K-sized integer will be 13486248624.

Here we can see the digits repeat from a3 in a cycle of 4 digits.

We can calculate a2 by a2=(a0+a1) mod 10.

The digit a3 can be written 2 * (a0+a1) mod 10 by replacing the value of a2 in the expression (a0+a1+a2) mod 10.

The digit a4 can be written as (a0+a1+a2+a3) mod 10 or 2 * (a0+a1) mod 10 + 2*(a0+a1) mod 10= 4 * (a0+a1) mod 10.

Similar to the previous example, a5=(a0+a1+a2+a3+a4) mod 10 = 8 * (a0+a1) mod 10 since a0+a1+a2+a3=a4 which makes (2*a4) mod 10.

Similarly, a6=(a0+a1+a2+a3+a4+a5) mod 10 = (2 * a5) mod 10 = 16 * (a0+a1) mod 10 = 6*(a0+a1) mod 10. In this expression 16 mod 10 can be written as 6.

The results are the same for the digits that were repeated in a cycle of four after the first three digits, as we shall see if we compute the number's further digits.

By computing the modulo of the power of 2 mod 10 times the sum of a0 and a1, which is 2, 4, 8, and 6, it is possible to identify the recurring digits in a number.

Therefore, the total sum of digits of the number where each digit is the sum of all prefix digits modulo 10 can be given by the below formula −

Sum =a0+a1+a2+m*((K-3)/4)+n

where,

m=sum of digits which repeats in cycle of 4.

(K-3)/4 = number of times the cycle repeats in the given number

n = sum of remaining digits which are not the complete cycle of repeating digits

We will use the above formula to calculate the sum of digits of the K-sized number and check if it is divisible by 3 or not in our approach to solve the problem.

Approach

  • We will make a boolean function to check if the number is the multiple of 3 or not.

  • In order to check if the given number is multiple of 3, we will find the sum of digits of the number and check if it is divisible by 3 or not.

  • Initialise a variable to store the value of m which is the sum of digits repeating in the cycle of 4.

  • The remaining cycle digits will then be determined using (K-3)%4 and stored in a separate variable. As the first three numbers are not part of the digits' repeating cycle, (K-3) is used instead.

  • We will employ a switch case to get the sum of the last digits that do not complete the cycle of repeated digits. If there are just two digits remaining, we will store the sum of 2*(a0+a1) mod 10 and 4*(a0+a1) mod 10 in n. The sum of the power of 2 mod 10 times the sum of the first two digits with regard to the power of 2 mod 10 repeating cycle will be stored similarly according to the remaining digits.

  • Now, we use the formula to calculate the sum of the digits of a K-sized integer, and we store the result in a variable called sum.

  • Then we will check if the sum is divisible by 3 or not. If it is divisible by 3, we print YES as it is the multiple of 3, else NO.

Example

The C++ code for the approach:

//c++ code to check if the number is multiple of 3 

#include <bits/stdc++.h>
using namespace std;

//function to check if the sum of all digits of K sized integer is divisible by 3 or not
bool Multipleof3(int a0,int a1,long long int K) {
   //to calculate value of m in the formula
   long long int m=(2*(a0+a1))%10+(4*(a0+a1))%10+(8*(a0+a1))%10+(6*(a0+a1))%10;
   
   //to calculate the remaining terms of the repeating cycle of 4 digits
   long long int rem=(K-3)%4;
   
   //to store the sum of remaining digits
   long long int n;
   
   //using switch case to calculate sum of remaining digits
   switch(rem){
      case 0: //when remaining digit is 0
         n=0;
         break;
           
      case 1: //when remaining digit is 1
         n=(2*(a0+a1))%10;
         break;
           
      case 2: //when remaining digits are 2
         n=(2*(a0+a1))%10+(4*(a0+a1))%10;
         break;
           
      case 3: //when remaining digits are 3
         n=(2*(a0+a1))%10+(4*(a0+a1))%10+(8*(a0+a1))%10;
         break;
   }
   
   long long int a2=(a0+a1)%10; //calculating the third digit of the number from left
   
   //using the formula a0+a1+a2+m*((K-3)/4)+n, calculating the sum of all the digits
   long long int sum=a0 + a1 + a2 + ((K-3)/4)*m + n;
   
   if(sum%3==0){ //checking if sum is divisible by 3
      return true;
   } else {
      return false;
   }
}

int main()
{
   int a0, a1;
   long long int K; //initialising the input variables
   
   a0=7;
   a1=5;
   K=16;
   
   //checking if function returns true
   if(Multipleof3(a0,a1,K)==true){
      cout<<"Yes"<<endl;
   } else {
      cout<<"No"<<endl;
   }
   return 0;
}

Output

Yes

Time Complexity : O(1), as the algorithm we used takes constant time to check if the number is multiple of 3 or not.

Space Complexity : O(1), because no extra space is taken to solve the problem.

Conclusion

The method to calculate the sum of digits of any K-sized integer where first two digits of the number from left are given and every digit is the sum of all prefix digits in the number was discussed in the article. Using the efficient method, we solve the problem of checking if the number is multiple of 3 or not where each digit is the sum of all the prefix digits modulo 10 in C++ within constant time and space.

I hope you have cleared all the concepts regarding the problem and understand the algorithm to solve the problem after reading this article.

Updated on: 21-Jun-2023

216 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements