A magic number is defined as a number which can be expressed as a power of 5 or sum of unique powers of 5
belongs to collection: interview C++ coding problems/challenges | Miscellaneous
All Answers
total answers (1)
belongs to collection: interview C++ coding problems/challenges | Miscellaneous
total answers (1)
One naïve approach is to use a set to store the sequence of magic numbers. We store the powers of 5, and then try all combination sum between the unique powers and insert the sums into the set if it already not there. Set sorts the insertion and so as the set size reaches to N, we can output the last number in the set/ largest no in the set.
Find finding sums between different combinations are tedious and N-P hard problem if solved in general.
So the solution is something different.
Let's manually compute the magic numbers first and try to observe the generated sequence as a whole.
Algo:
C++ implementation:
Output
need an explanation for this answer? contact us directly to get an explanation for this answer