ok so I went through that github link. I think it’s old. In test input three the last input is wrong. ( 2 9 5 )
Also my code passed all the other test cases but is still getting WA.
so my approach is this
I found a pattern in the number…
there is a repeating pattern of 8 6 2 4 from k=3
so I made a variable sum as (d0= 1st no and d1= 2nd no)
s= k/4
m=k%4
sum = (sum of digits of 3rd number) + s*(8+6+4+2)
now I add the remaining digits. For ex if last digit of k=4 is 8 then the next in line is 6 and then 2 and then 4
I think I ve covered everything, but still getting WA.
The code is:
import java.util.*;
import java.io.*;
import java.math.*;
class Rextester
{
static class Reader
{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1)
{
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.')
{
while ((c = read()) >= '0' && c <= '9')
{
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
public static void main(String[] args) throws IOException
{
Reader in=new Reader();
int test=in.nextInt();
while(test-- >0){
int k=in.nextInt();
int d0=in.nextInt(), d1=in.nextInt();
String D2=d0+""+d1+""+((d0+d1)%10);
//System.out.println(D2);
int d2=Integer.parseInt(D2);
String D3=d2+""+((d0+d1+d2)%10);
//System.out.println(D3);
int d3=Integer.parseInt(D3);
//check for k<=3
if(k==2){
if((d0+d1)%3==0)
System.out.println("YES");
else
System.out.println("NO");
}
else if (k==3) {
if((d2)%3==0)
System.out.println("YES");
else
System.out.println("NO");
}
else if (k==4) {
if((d3)%3==0)
System.out.println("YES");
else
System.out.println("NO");
}
else if (d3%10==0) {
if(d2%3==0)
System.out.println("YES");
else {
System.out.println("NO");
}
}
else{
int skip=8+6+2+4;
int skp[]={8, 6, 2, 4};
long summ=d0+d1+d2%10;
BigInteger sum= new BigInteger(String.valueOf(summ));
//System.out.println(sum);
k=k-4;int index=-1;
if(d3%10==8)
index=0;
if(d3%10==6)
index=1;
if(d3%10==2)
index=2;
if(d3%10==4)
index=3;
//long s= k/4;
// BigInteger s= new BigInteger((k/4).toString());
int m= k%4;
//sum= sum+s*skip;
sum= sum.add(BigInteger.valueOf((k/4)*skip));
//System.out.println(index);
for(int i=0;i<=m;i++){
int temp=skp[(index)%(skp.length)];
sum=sum.add(BigInteger.valueOf(temp));
//System.out.println("--> "+sum);
index++;
}
//System.out.println("sum: "+sum);
if(sum.mod(BigInteger.valueOf(3)).intValue() == 0)
System.out.println("YES");
else {
System.out.println("NO");
}
System.out.println("SUM: "+sum);
}
}
}
}
//https://github.com/strikersps/Competitive-Programming/tree/master/Code-Chef/Multiples-of-3/test-cases
I believe the code is correct. If anyone can please help me to find the test case for which it is failing, it would be a huge help.