Skip to content

Instantly share code, notes, and snippets.

@mmloveaa
Last active April 25, 2016 05:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmloveaa/d3324ade58310e96739ad82696e1fc9e to your computer and use it in GitHub Desktop.
Save mmloveaa/d3324ade58310e96739ad82696e1fc9e to your computer and use it in GitHub Desktop.
4-19 -Q4 Sherlock and The Beast
Sherlock Holmes and his dear friend Dr. Watson are working on an urgent problem. Watson had earlier learned through his "special relationship" contacts at MI6 that that the CIA has lately been facing weird problems with their supercomputer, 'The Beast'. Then, Sherlock received a note from Professor Moriarty, his archenemy, boasting that he has infected 'The Beast' with a virus.
Now, all of Sherlock's past efforts to subdue Moriarty had been in vain, but the note gave him new hope to finally triumph over his nemesis. You see, the note had been written on a piece of paper previously sat on Moriaty's desk under another piece of paper. Unbeknownst to anyone but the intrepid Sherlock Holmes, it had an impression of a number N on it. After following up with more clues from his past experience with Moriarty, Sherlock figured out that the key to remove the virus is the largest 'Decent' Number having N digits.
A 'Decent' Number has -
Only 3 and 5 as its digits.
The number of times 3 appears is divisible by 5.
The number of times 5 appears is divisible by 3.
Meanwhile, the trigger date of the virus to completely wipe the disk and burn out the processors of 'The Beast' is fast approaching. Can you save 'The Beast', and find the key for Sherlock?
Constraints
1 ≤ T ≤ 20
1 ≤ N ≤ 106
Input Format
The first line will contain an integer T, the number of test cases. This is followed by T lines, each containing an integer N i.e. the number of digits in the number
Output Format
Largest Decent number having N digits. If no such number exists, tell Sherlock that he misread his clues and print '-1' without quotes.
Sample Input #00
4
1
3
5
11
Sample Output #00
-1
555
33333
55555533333
Explanation #00
For N=1, there is no such number.
For N=3, 555 is only possible number.
For N=5, 33333 is only possible number.
For N=11, 55555533333 and all of permutations of its digits are valid, but this is the largest among them.
function decentNumber(arr) {
for(var i =0 ; i < arr.length; i++){
var fiveMults = 0;
var threeMults = 0;
var maxOne =0;
for(var j = 1; j < Math.floor(arr[i]/3);j++){
if((arr[i] - 3 * j) % 5 === 0){
fiveMults = j;
threeMults = (arr[i]-3*j)/5;
}
}
maxOne='5'.repeat(fiveMults*3)+'3'.repeat(threeMults*5);
if(arr[i]%3===0){
console.log('5'.repeat(arr[i]))
}
else if(maxOne!==""){
console.log(maxOne);
}
else if(arr[i]%5===0){
console.log('3'.repeat(arr[i]));
}
else{
console.log("-1");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment