Search This Blog

Sunday, April 13, 2014

Algorithms - Warmup - Sherlock and The Beast

 Sherlock and The Beast

The following is the solution to Hacker Rank problem Sherlock and The Beast using Java.  For solutions to other Hacker Rank Problem visit my page HackerRank, alternatively try searching for the problem in my blog.

Score:  30/30
/**
 *
 */


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author Arun.G
 *
 */
public class Solution{

       /**
        * @param args
        */
       static BufferedReader in = new BufferedReader(new InputStreamReader(
                     System.in));

       public static String repeat(String str, int times) {
              StringBuilder ret = new StringBuilder();
              for (int i = 0; i < times; i++)
                     ret.append(str);
              return ret.toString();
       }

       public static void main(String[] args) throws NumberFormatException,
                     IOException {
              int T = Integer.parseInt(in.readLine());
              for (int i = 0; i < T; i++) {

                     String s = "";

                     int N = Integer.parseInt(in.readLine());

                     for (int j = N; j >= 0; j--) {
                           if (j % 3 == 0 && (N - j) % 5 == 0) {
                                  s = repeat("5", j) + repeat("3", N - j);

                                  break;
                           }
                     }

                     if (s == "")
                           System.out.println("-1");

                     else
                           System.out.println(s);
                    
                     in.close();

              }
       }
}

No comments:

Post a Comment

Labels