Wednesday, March 23, 2016

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; however, he also gives him a clue—a number, NN. Sherlock determines the key to removing the virus is to find the largest Decent Number having NN 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. If there are more than one such number, we pick the largest one.
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!
Constraints
1T201T20
1N1000001N100000
Input Format
The first line is an integer, TT, denoting the number of test cases.
The TT subsequent lines each contain an integer, NN, detailing the number of digits in the number.
Output Format
Print the largest Decent Number having NN digits; if no such number exists, tell Sherlock by printing -1.
Sample Input
4
1
3
5
11
Sample Output
-1
555
33333
55555533333
Explanation
For N=1N=1, there is no decent number having 11 digit (so we print 11).
For N=3N=3, 555555 is the only possible number. The number 55 appears three times in this number, so our count of 55's is evenly divisible by 33 (Decent Number Property 3).
For N=5N=5, 3333333333 is the only possible number. The number 33 appears five times in this number, so our count of 33's is evenly divisible by 55 (Decent Number Property 2).
For N=11N=11, 5555553333355555533333 and all permutations of these digits are valid numbers; among them, the given number is the largest one.

---------------------------------------------------------------------------------------------------------

  Sherlock and The Beast -  Hacker Rank Solution

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

public class Solution {

  
        public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int t = in.nextInt();
    for(int a0 = 0; a0 < t; a0++){
        {
            int n = in.nextInt();
            StringBuilder strb=new StringBuilder();
            for(int i=n;i>=0;i--)

            {  
                if(i%3==0 && (n-i)%5==0)
                {
                    int j=0;
                    for(j=0;j<i;j++)
                        strb.append("5");
                    for(int k=j;k<n;k++)
                        strb.append("3");
                    break;

                }
            }
            if(strb.length()==0)
                System.out.println("-1");
            else
                System.out.println(strb);
        }
    }
}
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
   
}

  Sherlock and The Beast -  Hacker Rank Solution

No comments:

Post a Comment

Powered by Blogger.