Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

HackerRank Sherlock and The Beast problem solution

YASH PAL, 31 July 202424 January 2026

In this 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:

  1. Its digits can only be 3’s and/or 5’s.
  2. The number of 3’s it contains is divisible by 5.
  3. The number of 5’s it contains is divisible by 3.
  4. 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!

HackerRank Sherlock and The Beast problem solution

HackerRank Sherlock and The Beast problem solution in Python.

def decent_number(N):
    """
    Prints a decent number of N digits. -1 if no such number exists.
    """
    
    for fives in range(N, -1, -1):
        threes = N - fives
        if fives % 3 == 0 and threes % 5 == 0:
            return "5" * fives + "3" * threes
    return "-1"
    
def main():
    
    import sys
    
    count, *Ns = sys.stdin.readlines()
    
    for N in Ns:
        print(decent_number(int(N)))
    
if __name__ == "__main__":
    main()

Sherlock and The Beast problem solution in Java.

import java.io.*;
import java.util.*;

public class Solution {

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int count = scan.nextInt();
int piv;
for (int x = 0; x < count; x++) {
piv = scan.nextInt();
decent(piv, pivot(piv));
}
}

private static void decent(int n, int piv) {
if (piv < 0) {
System.out.println("-1");
}
else {
int repeat = piv/3;
while(repeat-- > 0)
System.out.print("555");
repeat = (n - piv)/5;
while(repeat-- > 0)
System.out.print("33333");
System.out.println();
}
}

private static int pivot(int n) {
while(n > 0) {
if(n % 3 == 0)
break;
else
n -= 5;
}
return n;
}
}

Problem solution in C++.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    int T;
    scanf("%d",&T);
    while (T--)
    {
        int n;
        scanf("%d",&n);
        int ans5=-1;
        for (int i=0;i*3<=n;i++)
        {
            int tot=n-3*i;
            if (tot%5==0)
                ans5=3*i;
        }
        if (ans5==-1) puts("-1");
        else
        {
            for (int i=0;i<ans5;i++)
                printf("5");
            for (int i=0;i<n-ans5;i++)
                printf("3");
            puts("");
        }
    }
    return 0;
}

Problem solution in C.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
    int T; int n;
    scanf("%d", &T);
    while(T--){
        scanf("%d", &n);
        int mfive = -1;
        for(int five = 0; five * 3 <= n; five++){
            if((n - five * 3)%5 == 0)
                mfive = five;
        }
        if(mfive == -1)
            printf("%dn", -1);
        else{
            for(int i = 0; i < mfive * 3; i++)
                putchar('5');
            for(int i = mfive * 3; i < n; i++)
                putchar('3');
            puts("");
        }
    }
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    return 0;
}

Algorithms coding problems solutions AlgorithmsHackerRank

Post navigation

Previous post
Next post
CLOSE ADS
CLOSE ADS

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes