Why long datatype?

Why does the below code not pass test cases when integer datatype is used?

public class Main
	public static void main(String[] args) throws IOException
			//Your Solve
		Scanner in=new Scanner(System.in);
		int t=in.nextInt();
		  //  //ps: this is the health of the monster.
		  //  //for us to win the game, the monster's health points gotto be 0 or lesser.
		        //y represents gain of points by monster, x=loss by monster..;, 
		        //ideally for us to win the monster's loss should be more ...otherwise we are losing, we wont defeat the bad guy.
		    long h = in.nextLong();
		    long x = in.nextLong();
		    long y = in.nextLong();
		    if (x>y)
		}catch(Exception e){

can you link the full problem? I would assume the numbers read are too big to be stored inside integers.

1 Like

PLEASE CHECK out the above link!

you can print the min and max values of both int and long in java like this:

System.out.println(Integer.MIN_VALUE); //-2147483648
System.out.println(Integer.MAX_VALUE); //2147483647
System.out.println(Long.MIN_VALUE); //-9223372036854775808
System.out.println(Long.MAX_VALUE); //9223372036854775807

You do not have to learn these values though. It is easier to keep in mind that an int has 32 bits and a long has 64 bits worth of storage. This corresponds to a size of 2³² and 2⁶⁴ respectively.

The problem states you need to be able to store 10¹⁸.
10³ 2¹⁰ =>10¹⁸ 2⁶⁰ => you need long