Query regarding a problem

I got wrong answer when I used int to store the values of n, a, b and total score of both, but as soon as I switched int to long my answer was accepted. why I dont know.
please help.

Contest Link: Equinox Strings | CodeChef

Code:
import java.util.Scanner;
public class EQUINOX {
public static void main(String[] args) {

``````    Scanner s = new Scanner(System.in);
int t = s.nextInt();
while(t-- > 0){
long n = s.nextLong();
long a = s.nextLong();
long b = s.nextLong();
long STotal = 0, ATotal = 0;
s.nextLine();

for(int i=0;i<n;i++){
char ch = s.nextLine().charAt(0);
if(ch == 'E' || ch == 'Q' || ch == 'U' || ch == 'I' || ch == 'N' || ch == 'O' || ch == 'X'){
STotal += a;
}else{
ATotal += b;
}
}
if(STotal == ATotal){
System.out.println("DRAW");
}else if(ATotal > STotal){
System.out.println("ANURADHA");
}else{
System.out.println("SARTHAK");
}
}
}
``````

}

1 Like

It might happen that total score of both can be in the range of long long because when you multiply a or b with n and then again with test cases you will get total score in range of long long.

due to INT overflow problem your solution was not accepted . As soon as you switch to long or long long the INT overflow situation was controlled . link your code to your post .

So how do I find when to use INT and when to use LONG or DOUBLE.

in CP you can always use long long , just so that you don’t come across any overflow/
here is a link , you can check the ranges int , double ,long long etc …

Don’t use long long unnecessarily if the result can fit in int. You might get tle in some problems

so how do I decide when to use int and when to use long.

While solving any question you should keep track of the magnitude of the calculations you will be performing and on the basis of that you can select the data type.
Sometimes the constraints of the problem may be small but if are performing operations like multiplication or exponentiation which may be greater than range of INT then you can use bigger data types.

Are there any set of constraints where INT is sufficient or do I need to use trial and error method every-time.

This should help.