Multiple of 3 problem

I am getting a problem with the pattern. I am assuming that the pattern appears after the 1st 3 digits, as is the case in all given test cases. And the pattern is always made up of 4 digits. With the digits being 2,4,6,8. Now the problem is, I was using only the first 3 digits, the pattern and the extra numbers to check divisibility.

Example, for the test case,(13 8 1)
The first 3 digits are 819, the pattern is 8624, and the extra digits are 86. So the final number becomes 8198624862486.

Now, with my solution, I was trying to check divisibility by just using the pattern once and then the extra digits. For the above example, I would only check for 819862486. The 1st 3 digits, the pattern only once and the extra digits. But it is not working. It’s printing NO for all the given test cases. Maybe I am checking for the wrong number.I am pasting the code for reference. There are some print statements that I used for checking. Ignore them.

Please tell me some suggestions. Thank you.

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

int main()
{
	int t;
	cin>>t;
	while(t!=0)
	{
		t--;
		long long int k,d1,d2;
		cin>>k>>d1>>d2;
		long long int d3=(d1+d2)%10;
		long long int sum=d1+d2+d3;
		long long int first_three=d3+d2*10+d1*100;
		long long int n=4;
		vector<long long int> v;
		while(n!=0)
		{
			n--;
			long long int d_temp=sum%10;
			v.push_back(d_temp);
			sum=sum+d_temp;	
		}
		
		long long int pattern=v[3]+v[2]*10+v[1]*100+v[0]*1000;
		long long int final_num=first_three*10000+pattern;
		//cout<<final_num<<" ";
		if(k>7)
		{
			k=(k-3)%4;
			//cout<<k<<endl;
			//cout<<final_num;
			
			if(k==1)//1 digit
			{
				final_num=final_num*10+v[0];
			}
			else if(k==2)//2 digit
			{
				final_num=final_num*100+v[0]*10+v[1];
			}
			else if(k==3)//3 digit
			{
				final_num=final_num*1000+v[0]*100+v[1]*10+v[2];
			}
			else if(k==0)//4 digit,no need to append
			{
				continue;
			}
			cout<<k<<endl<<final_num;
			if(final_num%3==0)
			{
				cout<<"YES"<<endl;
			}
			else
			{
				cout<<"NO"<<endl;
			}
			
		}
		else
		{
			final_num=final_num/(int(pow(10,7-k)));
			//cout<<final_num<<endl;
			if(final_num%3==0)
			{
				cout<<"YES"<<endl;
			}
			else
			{
				cout<<"NO"<<endl;
			}
		}
		
		
	}
	return 0;
}

Check the problem discussion here: - YouTube at 1:15:00

1 Like