We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
A bit of pseudo code below on how I solved this. The trick, really, is to just analyse the input numbers and to avoid actually generating the final answer until the end. Hopefully this will help those of you who are stuck:
Let's look at two numbers: the numbers of fives (F) and the number of threes (T). For the purposes below "n" will be the inputted number length.
The default biggest number will be one consisting of all 5s, so lets start out with F = n and T = 0. So our number right now consists of all 5s and no 3s.
Is the number compliant? Test: F is a multiple of 3 and T is a multiple of five. Answer: If Yes: return F and T, BUT IF No:
We need to replace some 5s with 3s, and since the 3s must be in multiples of 5, we need to reduce F by 5 and increase T by 5, so: F = F - 5 and T = T + 5.
After we decrease F, is it less than zero? If so, we have a bad number and we can return -1. IF NOT:
Lets recursively run through our steps again, and jump back to step #2, and continue doing so until we hit one of our two return conditions.
Once we've hit a return statement in the above, we just need to use the numbers either -1 or F and T, to generate a print statement.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Sherlock and The Beast
You are viewing a single comment's thread. Return to all comments →
A bit of pseudo code below on how I solved this. The trick, really, is to just analyse the input numbers and to avoid actually generating the final answer until the end. Hopefully this will help those of you who are stuck:
Let's look at two numbers: the numbers of fives (F) and the number of threes (T). For the purposes below "n" will be the inputted number length.
The default biggest number will be one consisting of all 5s, so lets start out with F = n and T = 0. So our number right now consists of all 5s and no 3s.
Is the number compliant? Test: F is a multiple of 3 and T is a multiple of five. Answer: If Yes: return F and T, BUT IF No:
We need to replace some 5s with 3s, and since the 3s must be in multiples of 5, we need to reduce F by 5 and increase T by 5, so: F = F - 5 and T = T + 5.
After we decrease F, is it less than zero? If so, we have a bad number and we can return -1. IF NOT:
Lets recursively run through our steps again, and jump back to step #2, and continue doing so until we hit one of our two return conditions.
Once we've hit a return statement in the above, we just need to use the numbers either -1 or F and T, to generate a print statement.