My issue
well , the main thing is the number should be divisible by 3 and not by 9 , i initialized the number to min odd number of n digits ( let n = 4 ) then num = 1001,
now i will inc it by 2 every time ( to keep it odd ) and when num%3 == 0 && num%9 !=0 then cout the num , what i am doing wrong here
also num is 'long long ’
My code
#include <iostream>
using namespace std;
int main() {
int t;
cin>>t;
while(t--){
int n ; cin>>n;
long long num = 1;
if(n==1){
cout<<3<<endl;
continue;
}
n--;
while(n--){
num *= 10;
}
num += 1;
// now we have our number ready , the min odd no of n digits
//e.g. if n=5 --> num = 10001
while(true){
if(num%3 == 0 && num%9 != 0 ){break;}
num += 2 ; // so that it always remains odd
// add solution is sure to exist , so it won't be an infinite loop
}
cout<<num<<endl;
}
return 0;
}
Learning course: Jump from 2* to 3*
Problem Link: Make it Divisible Practice Problem in Jump from 2* to 3* - CodeChef