I was solving a question on Codechef and I encountered this problem.
Here is the link to the question. https://www.codechef.com/LP0TO101/problems/FLOW013
Basically we are given three angles and we need to check if their sum is 180 and therefore it is a triangle, and if it is we need to print YES or else we need to print NO.
We are given input in the following format.
3
40 40 100
45 45 90
180 1 1
[Note: There is a blank string(" ") right after 3.]
This is my source code.
import java.util.Scanner;
import java.util.stream.IntStream;
class Codechef {
public static void main(String[] args) throws java.lang.Exception {
Scanner sc = new Scanner(System.in);
int T = Integer.parseInt(sc.nextLine().split(" ")[0]);
String[] output = new String[T];
while (--T >= 0) {
String arr[] = sc.nextLine().split(" ");
System.out.println("arr[0]: " + Integer.parseInt(arr[0]));
System.out.println("arr[1]: " + Integer.parseInt(arr[1]));
System.out.println("arr[2]: " + Integer.parseInt(arr[2]));
if ((Integer.parseInt(arr[0]) + Integer.parseInt(arr[1]) + Integer.parseInt(arr[2])) == 180) {
output[T] = "YES";
} else {
output[T] = "NO";
}
}
IntStream.range(0, output.length).forEach(h -> System.out.println(output[h]));
}
}
The output it is returning on Codechef is:
arr[0]: 40
arr[1]: 40
arr[2]: 100
arr[0]: 45
arr[1]: 45
arr[2]: 90
arr[0]: 180
arr[1]: 1
arr[2]: 1
NO
YES
YES
What is wrong here, please help.
My question is how come arr[0]: 40 + arr[1]: 40 + arr[2]: 100 is not equal to 180, I mean 40 + 40 + 100 should be equal to 180, so what is wrong.
When in the Eclipse IDE I gave the input of:
1
40 40 100
It showed me output of:
arr[0]: 40
arr[1]: 40
arr[2]: 100
YES
But when I give the same output there, it doesn't executes fully and keeps processing while it is incomplete and shows me output of(I am inserting the image):

Note: The red square in the right corner of the image shows that it is still executing.
However when I have removed the printing statements and gave the same input in eclipse, it showed me an output of: (Inserting an image again)

I hope you have understood my problem.
My question again is how come arr[0]: 40 + arr[1]: 40 + arr[2]: 100 is not equal to 180, I mean 40 + 40 + 100 should be equal to 180, what is wrong. And when I am executing it individually it is showing me the right output but when I do the test case on Codechef it is showing the opposite answer.
What is wrong here?
The variable
Truns "backwards"; it starts at - in the example -2and ends at0. VariableTis also used to index theoutputarray. This means that:output[2]correlates with the 1st iteration (i.e. the input40 40 100),output[1]correlates with the 2nd iteration (i.e. the input45 45 90), andoutput[0]correlates with the 3rd iteration (i.e. the input180 1 1).Now we see that - in fact - the value of
output[0](being"NO") is correct since180 + 1 + 1 = 182 != 180.I rewrote the program such that the
outputis stored in the order the values are calculates:This then gives the output:
Ideone.comdemoSome remarks:
We should use descriptive names for variables (e.g.
String[] arrcould beString[] userInputs)variable names should be writte in
lowerCamelCase(int T = ...->int t = ...)Instead of parsing the
Strings over and over again, I would suggest to parse them once, and then - for example - store them in aList<Integer>and work with them, e.g.:While possible, it is uncommon to write the array-brackets (
[]) after the variable name. We normally write them after the type since they influence the type (String arr[]->String[] arr).