Help me in solving MAKEDIV3 problem

My issue

i cant find whats wronge with this code

My code

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

int main() {
	// your code goes here
	int t;
	cin >> t;
	while(t--){
	    int n;
	    cin >> n;
	    int X = 3;
	    if(n == 1){}
	    else if(n == 2)X = X + 12;
	    else{
	        X = 0;
	        for(int i = 0;i < n-2;i++){
	            X = (X * 10) + 1;
	        }
    	    if((n-2)%3 == 0 && (n+1)%9 != 0)X = X*100 + 21;
	        else if((n-2)%3 == 1 && n%9 != 0)X = X*100 + 11;
	        else if((n-2)%3 == 2 && (n+2)%9 != 0)X = X*100 + 31;
	    }
	    cout << X << endl;
	}

}

Learning course: Jump from 2* to 3*
Problem Link: Make it Divisible Practice Problem in Jump from 2* to 3* - CodeChef

Your code is overflowing your X variable.

Note that an integer barely handles a number of 9 digits, and a long long one of 18 digits.

What will happen with an input of N = 1000?

so what can we do in that case

cout << “3\n”;

and

cout << 3 << “\n”;

works for the same purpose.

Arithmetic force is almost never the answer in huge digits problems. Try thinking on strings or another representation.

1 Like

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

int main() {
// your code goes here
int t;
cin >> t;
while(t–){
long long int n;
cin >> n;
int X = 3;
if(n == 1){}
else if(n == 2)X = X + 12;
else{
X = 0;
for(int i = 0;i < n-2;i++){
cout << “1”;
}
if((n-2)%3 == 0 && (n+1)%9 != 0)X = X100 + 21;
else if((n-2)%3 == 1 && n%9 != 0)X = X
100 + 11;
else if((n-2)%3 == 2 && (n+2)%9 != 0)X = X*100 + 31;
}

    cout << X << endl;
    
}

}
it is still giving error

Try this:

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

int main() {

    int t;
    cin >> t;
    while(t--){
        long long int n;
        cin >> n;
        int X = 3;
        
        if(n == 1){}
        else{
            cout << X;
            for(long long i=0; i<n-2; i++){
                cout << "0";
            }
        }
        cout << X << endl;
    }
}
1 Like

ya that one is better no if else conditions required