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,N . Sherlock determines the key to removing the virus is to find the largest Decent Number having N digits.
A Decent Number has the following properties:
Constraints
1≤T≤20
1≤N≤100000
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,
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.
- If there are more than one such number, we pick the largest one.
Constraints
Input Format
The first line is an integer, T , denoting the number of test cases.
TheT subsequent lines each contain an integer, N , detailing the number of digits in the number.
The
Output Format
Print the largest Decent Number having N 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=1 , there is no decent number having 1 digit (so we print −1 ).
ForN=3 , 555 is the only possible number. The number 5 appears three times in this number, so our count of 5 's is evenly divisible by 3 (Decent Number Property 3).
ForN=5 , 33333 is the only possible number. The number 3 appears five times in this number, so our count of 3 's is evenly divisible by 5 (Decent Number Property 2).
ForN=11 , 55555533333 and all permutations of these digits are valid numbers; among them, the given number is the largest one.
---------------------------------------------------------------------------------------------------------
For
For
For
---------------------------------------------------------------------------------------------------------
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. */
}
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. */
}
No comments:
Post a Comment