While solving a problem from HackerRank, though it's an easy one, I wrote this big program. Not sure if recursion is applicable and hold good but to me it looked like it isn't. However when I run the program I see my code executed fairly well.
Here is the problem statement ("Sherlock and The Beast"):
Print the largest decent number. A 'Decent' Number can have:
- Only 3 and 5 as its digits.
- Number of times 3 appears is divisible by 5.
- Number of times 5 appears is divisible by 3.
- Input should be the number of digits (N) where 1 <= N <= 100000.
For 10 such test cases if I have the inputs:
3647
8884
1233
99999
130
11111
3455
23454
123211
345
My code ran in 0.008688 seconds. Is there any room for improvement?
int filldigits(num) {
int max_div, num_of_5s, num_of_3s;
if (num/3 == 0) {
num_of_5s = num;
print(num_of_5s, 0);
return 0;
}
max_div = num/3;
for (max_div; max_div>0; max_div--) {
num_of_5s = max_div * 3;
num_of_3s = num - num_of_5s;
if (num_of_3s % 5 == 0 ) {
print(num_of_5s, num_of_3s);
return 0;
}
else
continue;
}
max_div = num/5;
for (max_div; max_div>=1; max_div--) {
num_of_3s = max_div * 5;
num_of_5s = num - num_of_3s;
if (num_of_5s % 3 == 0) {
print(num_of_5s, num_of_3s);
return 0;
}
else
continue;
}
return -1;
}