Today we will be Solving Multiple of 3 CodeChef Problem which is a part of CodeChef DSA Learning Series.

Multiple of 3 CodeChef Solution
Problem
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 we can’t give it to you on the input explicitly; instead, you are only given its starting digits 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, the following formula must hold:
Determine if N is a multiple of 3.
Input Format
The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
The first and only line of each test case contains three space-separated integers K, d0 and d1.
Output Format
For each test case, print a single line containing the string “YES” (without quotes) if the number N is a multiple of 3 or “NO” (without quotes) otherwise.
Constraints
- 1 ≤ T ≤ 1000
- 2 ≤ K ≤ 1012
- 1 ≤ d0 ≤ 9
- 0 ≤ d1 ≤ 9
Sample Input
3 5 3 4 13 8 1 760399384224 5 1
Sample Output
NO YES YES
Explanation
Example case 1: The whole number N is 34748, which is not divisible by 3, so the answer is NO.
Example case 2: The whole number N is 8198624862486, which is divisible by 3, so the answer is YES.
Multiple of 3 CodeChef Solution in Java
import java.util.Scanner; class MULTHREE { public static void solve(long k,int d0,int d1){ StringBuffer sb=new StringBuffer(""+d0+d1); long sum=d1+d0; if(k>=3){ if(sum%5!=0){ sum=sum+(sum%10); for(long i=(k-3)%4;i>0;i--){ sum=sum+(sum%10); } sum=sum+((k-3)/4)*20; }else { System.out.println("NO"); return; } } if(sum%3==0){ System.out.println("YES"); }else { System.out.println("NO"); } } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int T=sc.nextInt(); while (T-->0){ int d0,d1; long k; k=sc.nextLong(); d0=sc.nextInt(); d1=sc.nextInt(); solve(k,d0,d1); } } }
Multiple of 3 CodeChef Solution in CPP
#include <bits/stdc++.h> #define mid(l,u) ((l+u)/2) #define MOD 1000000007 #define INF 10000000000000 #define int long long using namespace std; bool mulofthree(int n, int a, int b){ int sum = a+b; if(sum==1 || sum%10==0 || sum%10==5) return false; if(n==2) sum = a+b; else{ sum+=sum%10; int rem = (n-3)>=0 ? (n-3)%4 : 0; int q = (n-3)>=0 ? (n-3)/4 : 0; sum+=q*20; while(rem--){ sum+=sum%10; } } if(sum % 3==0) return true; else return false; } signed main() { int t; cin>>t; while(t--){ int n, a, b; cin>>n>>a>>b; if(mulofthree(n,a,b)) cout<<"YES\n"; else cout<<"NO\n"; } return 0; }
Multiple of 3 CodeChef Solution in Python
Comming soon
Disclaimer: The above Problem (Multiple of 3 CodeChef ) is generated by CodeChef but the solution is provided by Chase2learn.This tutorial is only for Educational and Learning purpose.