HackerRank Sherlock and The Beast Solution
In this pot, we will solve HackerRank Sherlock and The Beast Problem Solution.
Sherlock Holmes suspects his archenemy Professor Moriarty is once again plotting something diabolical. Sherlock’s companion, Dr. Watson, suggests Moriarty may be responsible for MI6’s recent issues with their supercomputer. The Beast.
Shortly after resolving to investigate, Sherlock receives a note from Moriarty boasting about infecting The Beast with a virus. He also gives him a clue: an integer. Sherlock determines the key to removing the virus is to find the largest Decent Number having that number of digits.
A Decent Number has the following properties:
- Its digits can only be 3’s and/or 5’s.
- The number of 3’s it contains is divisible by 5.
- The number of 5’s it contains is divisible by 3.
- It is the largest such number for its length.
Moriarty’s virus shows a clock counting down to The Beast’s destruction, and time is running out fast. Your task is to help Sherlock find the key before The Beast is destroyed! For example, the numbers 55533333 and 555555 are both decent numbers because there are 3 5’s and 5 3’s in the first, and 6 5’s in the second. They are the largest values for those length numbers that have proper divisibility of digit occurrences.
Function Description
Complete the decentNumber function in the editor below.
decentNumber has the following parameter(s):
- int n: the length of the decent number to create
Prints
Print the decent number for the given length, or -1 if a decent number of that length cannot be formed. No return value is expected.
Input Format
The first line is an integer, t, the number of test cases.
The next t lines each contain an integer n, the number of digits in the number to create.
Sample Input
STDIN Function
----- --------
4 t = 4
1 n = 1 (first test case)
3 n = 3 (second test case)
5
11
Sample Output
-1
555
33333
55555533333
Explanation
For n = 1, there is no Decent Number having 1 digit, so print -1.
For n = 3. 555 is the only possible number. (Decent Number Property 3).
For n = 5, 33333 is the only possible number. (Decent Number Property 2).
For n = 11,55555533333 is the Decent Number. All other permutations of these digits are not decent (Decent Number Property 4).
Sherlock and The Beast C Solution
# include <stdio.h>
int main(){
int t;
scanf("%d",&t);
while(t>0){
long long int i,x,y,n;
scanf("%llu",&n);
x = n/3;
y = 0;
while(x>=0){
if((n-3*x)%5 == 0){
y = (n-3*x)/5;
break;
}
x--;
}
if(x == -1 && y == 0)
printf("-1");
else{
for(i = 0; i < x; i++)
printf("555");
for(i = 0;i < y; i++)
printf("33333");
}
printf("\n");
t--;
}
return 0;
}
Sherlock and The Beast C++ Solution
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
void printDigit(int d, int times) {
for (int i = 0; i < times; i++) {
cout << d;
}
}
int main() {
int T, N;
cin >> T;
for (int t = 0; t < T; t++) {
cin >> N;
if (N == 1 || N == 2 || N == 4 || N == 7) {
cout << -1 << endl;
continue;
}
if (N % 3 == 0) {
printDigit(5, N);
} else if (N % 3 == 1) {
printDigit(5, N - 10);
printDigit(3, 10);
} else {
printDigit(5, N - 5);
printDigit(3, 5);
}
cout << endl;
}
return 0;
}
Sherlock and The Beast C Sharp Solution
using System;
using System.Collections.Generic;
using System.IO;
class Solution {
static void Main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
int t = int.Parse(Console.ReadLine());
List<int> inputs = new List<int>();
for(int i=0;i<t;i++){
inputs.Add(int.Parse(Console.ReadLine()));
}
foreach(var input in inputs){
bool success = false;
for(int i=input;i>=0;i--){
if(i%3==0 && (input-i)%5==0){
String fives = new String('5',i);
String threes = new String('3', (input-i));
Console.WriteLine(fives+threes);
success=true;
break;
}
}
if(!success){
Console.WriteLine("-1");
}
}
}
}
Sherlock and The Beast Java Solution
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
/*
* Complete the 'decentNumber' function below.
*
* The function accepts INTEGER n as parameter.
*/
public static void decentNumber(int n) {
// Write your code here
StringBuilder sb = new StringBuilder();
if(n % 3 == 0){
populate(sb, "5", n);
System.out.println(sb.toString());
return;
}else{
int maxFives = (n / 3) * 3;
for(int i=maxFives; i>-1; i-=3){
if((n - i) % 5 == 0){
populate(sb, "5", i);
populate(sb, "3", n-i);
System.out.println(sb.toString());
return;
}
}
}
System.out.println("-1");
}
public static void populate(StringBuilder sb, String num, int count){
while(count-- > 0){
sb.append(num);
}
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(bufferedReader.readLine().trim());
IntStream.range(0, t).forEach(tItr -> {
try {
int n = Integer.parseInt(bufferedReader.readLine().trim());
Result.decentNumber(n);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
});
bufferedReader.close();
}
}
Sherlock and The Beast JavaScript Solution
function processData(buffer) {
console.log(buffer.split('\n').slice(1).map(function(n) {
for(var p = n; p>=0; p-=5)
if (!((p)%3))
return Array.apply(null, {length: n}).map(Number.call, function(x) {
return x < p ? 5 : 3;
}).join('');
return "-1";
}).join('\n'));
}
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});
Sherlock and The Beast Python Solution
from math import floor
def sherlock(N):
for j in range(0, N+1, 5):
if (N-j) % 3 == 0:
return ("5"*(N-j)+"3"*j)
return -1
def main():
T = int(input())
for i in range(T):
N = int(input())
s = sherlock(N)
print(s)
main()