We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I was a bit confused and decided to use BigInteger and comparisons. In any case, this task requires more tools than just primitive data types. Here's my solution.
public static void main(String[] args) {
var scanner = new Scanner(System.in);
int cases = scanner.nextInt();
String[] values = new String[cases];
scanner.nextLine();
for (int i = 0; i < cases; i++) {
values[i] = scanner.nextLine();
}
for (int i = 0; i < cases; i++) {
BigInteger bi = new BigInteger(values[i]);
if (bi.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0 || bi.compareTo(BigInteger.valueOf(Long.MIN_VALUE)) < 0) {
System.out.println(values[i] + " can't be fitted anywhere.");
} else {
System.out.println(values[i] + " can be fitted in:");
if (bi.compareTo(BigInteger.valueOf(Byte.MAX_VALUE)) <= 0 && bi.compareTo(BigInteger.valueOf(Byte.MIN_VALUE)) >= 0) {
System.out.println("* byte");
}
if (bi.compareTo(BigInteger.valueOf(Short.MAX_VALUE)) <= 0 && bi.compareTo(BigInteger.valueOf(Short.MIN_VALUE)) >= 0) {
System.out.println("* short");
}
if (bi.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) <= 0 && bi.compareTo(BigInteger.valueOf(Integer.MIN_VALUE)) >= 0) {
System.out.println("* int");
}
System.out.println("* long");
}
}
scanner.close();
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Datatypes
You are viewing a single comment's thread. Return to all comments →
I was a bit confused and decided to use BigInteger and comparisons. In any case, this task requires more tools than just primitive data types. Here's my solution.