Sherlock and The Beast in Algorithm | HackerRank Programming Solutions | HackerRank Problem Solving Solutions in Java [💯Correct]

Hello Programmers/Coders, Today we are going to share solutions of Programming problems of HackerRank, Algorithm Solutions of Problem Solving Section in Java. At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.

In this post, you will find the solution for Sherlock and The Beast in Java-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.

Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.

Introduction To Algorithm

The word Algorithm means “a process or set of rules to be followed in calculations or other problem-solving operations”. Therefore Algorithm refers to a set of rules/instructions that step-by-step define how a work is to be executed upon in order to get the expected results. 

Advantages of Algorithms:

  • It is easy to understand.
  • Algorithm is a step-wise representation of a solution to a given problem.
  • In Algorithm the problem is broken down into smaller pieces or steps hence, it is easier for the programmer to convert it into an actual program.

Link for the ProblemSherlock and The Beast – Hacker Rank Solution

Sherlock and The Beast – Hacker Rank 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.

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!

For example, the numbers  and  are both decent numbers because there are  ‘s and  ‘s in the first, and  ‘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


Print the decent number for the given length, or  if a decent number of that length cannot be formed. No return value is expected.

Input Format

The first line is an integer, , the number of test cases.

The next  lines each contain an integer , 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)

Sample Output



For , there is no Decent Number having  digit, so print .
For ,  is the only possible number. (Decent Number Property 3).
For ,  is the only possible number. (Decent Number Property 2).
For ,  is the Decent Number. All other permutations of these digits are not decent (Decent Number Property 4).

Sherlock and The Beast – Hacker Rank Solution
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(;
        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) {
        else {
            int repeat = piv/3;
            while(repeat-- > 0)
            repeat = (n - piv)/5;
            while(repeat-- > 0)
    private static int pivot(int n) {
        while(n > 0) {
            if(n % 3 == 0)
                n -= 5;
        return n;

Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker🙏.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock