Problem Statement
A description of the problem can be found on Hackerrank.
Solution
Create two number: one of them represents fives_count
and the second <.code>threes_count. Initialize fives_count
as length of the number and threes_count
as 0
. Check if fives_count
is divisible by 3
and threes_count
is divisible by 5
. Then substract 5
from fives_count
and add 5
to threes_count
Continue until condition holds and print this number. If it is not possible return -1
.
We want to get the biggest number from all permutations. If we start the iteration with maximum count of fives, we are sure that if the condition holds, we get the biggest number.
I created solution in:
All solutions are also available on my GitHub.
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
object SherlockAndBeast extends App { val numbers = io.Source.stdin.getLines().toList.drop(1).map(_.toInt) val outputs = numbers.map(num => createOutput(divide(num, (num, 0)))) println(outputs.mkString("\n")) def divide(num: Int, tup: (Int, Int)): (Int, Int) = { if(tup._1 % 3 == 0 && tup._2 % 5 == 0) tup else if(tup._1 >= 0 && tup._2 <= num) divide(num, (tup._1 - 5, tup._2 + 5)) else (-1, -1) } def createOutput(tup: (Int, Int)): String = { if(tup._1 == -1) "-1" else "5" * tup._1 + "3" * tup._2 } } |
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
import java.util.Arrays; import java.util.Scanner; public class SherlockBeast { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); int tests = stdin.nextInt(); for(int i = 0; i < tests; i++) { int num = stdin.nextInt(); int fives = fiveCount(num); System.out.println(createOutput(num, fives)); } stdin.close(); } private static int fiveCount(int num) { int fives = num; while (fives >= 0 && (num - fives) <= num) { if(fives % 3 == 0 && (num - fives) % 5 == 0) { return fives; } fives -= 5; } return -1; } private static String createOutput(int num, int fives) { if(fives == -1) { return "-1"; } else { char[] fivess = new char[fives]; char[] threes = new char[num - fives]; Arrays.fill(fivess, '5'); Arrays.fill(threes, '3'); return new StringBuilder().append(fivess).append(threes).toString(); } } } |
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
function processData(input) { var numbers = input.split('\n').map(function(item) parseInt(item)); for(var i = 1; i < numbers.length; i++) { var fives = fiveCount(numbers[i]); if(fives === -1) { console.log('-1'); } else { console.log(new Array(fives).join('5') + new Array(numbers[i] - fives).join('3')); } } } function fiveCount(num) { var fives = num; while (fives >= 0 && (num - fives) <= num) { if(fives % 3 == 0 && (num - fives) % 5 == 0) { return fives; } fives -= 5; } return -1; } process.stdin.resume(); process.stdin.setEncoding("ascii"); _input = ""; process.stdin.on("data", function (input) { _input += input; }); process.stdin.on("end", function () { processData(_input); }); |
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
def remainders(number) fives = number threes = number - fives result = Array.new(2) while fives >= 0 && threes <= number do result[0] = fives result[1] = threes return result if(fives % 3 == 0 && threes % 5 == 0) threes += 5 fives -= 5 end result[0] = -1 result[1] = -1 return result end test_cases = gets.chomp.to_i test_cases.times do number = gets.chomp.to_i (fives, threes) = remainders(number) if fives == -1 puts "-1" else result = "" fives.times { result += "5" } threes.times { result += "3" } puts result end end |