The Sherlock and the Beast challenge from HackerRank asks to generate the following number with \$n\$ digits for \$T\$ test cases:
A Decent Number has the following properties: its digits can only be 3's and/or 5's. The number of 3's it contains is divisible by 5. The number of 5's it contains is divisible by 3. If there are more than one such number, we pick the largest one.
My code may not be very optimal. Can you please point out which test cases it may fail, as I may have missed a few conditions? Any suggestions to optimize the code?
using namespace std;
#include<iostream>
int main() {
int T;
cin >> T;
while (T >= 1) {
int n, flag;
cin >> n;
int z = n;
if(n%5==0){
for(int r=1;r<=n;r++){
cout<<"3";
}cout<<"\n";
}
else if(n%5!=0){
for (int i = 3; i <= n; i += 3) {
if ((z - i) % 5 == 0) {
flag = i;
break;
}
}
if (flag == 0|| (n-flag)%5!=0) {
cout << "-1"<<"\n";
}
else {
for (int x = 1; x <= flag; x++) {
cout << "5";
}
for (int y = 1; y <= (n - flag); y++) {
cout << "3";
}
cout<<"\n";
}
}
T--;
}
}