Monday, 27 April 2020

Sherlock and The Beast

The total number of digits (n) must be a linear multiple of 3 and 5, of form 3a + 5b = n where a is total number of 5's and b is total number of 3's. For the number to be largest possible, 'a' must be largest possible, so we can arrange 'a' 5's then remaining 'b' 3's and be done. Now, an upper bound on the number of 5's must be r= floor(n/3). the remaining digits are (b = n - r). If b % 5 == 0, then we are good! but if b % 5 != 0, then we must increment 'b' and decrement 'a' until either a == 0 without obtaining any "decent number" (as explained in question) where we return -1. If a "Decent number" exists, we shall obtain the correct distribution when both 'a' and 'b' become valid multiples of 3 and 5 respectively again. Return this number.

No comments:

Post a Comment