Search code examples
javastringadditioninteger-arithmeticstring-conversion

Can't we convert string into Integer and add it to other Integers at the same time and check in a if condition if it equals another integer?


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):

enter image description here

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)

enter image description here

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?


Solution

  • The variable T runs "backwards"; it starts at - in the example - 2 and ends at 0. Variable T is also used to index the output array. This means that:

    • output[2] correlates with the 1st iteration (i.e. the input 40 40 100),
    • output[1] correlates with the 2nd iteration (i.e. the input 45 45 90), and
    • output[0] correlates with the 3rd iteration (i.e. the input 180 1 1).

    Now we see that - in fact - the value of output[0] (being "NO") is correct since 180 + 1 + 1 = 182 != 180.

    I rewrote the program such that the output is stored in the order the values are calculates:

    class Ideone {
      public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = Integer.parseInt(sc.nextLine().split(" ")[0]);
        String[] output = new String[num];
        for (int index = 0; index < num; ++index) {
          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[index] = "YES";
          } else {
            output[index] = "NO";
          }
        }
        System.out.println(Arrays.toString(output));
      }
    }
    

    This then gives the output:

    ...
    [YES, YES, NO]
    

    Ideone.com demo


    Some remarks:

    • We should use descriptive names for variables (e.g. String[] arr could be String[] 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 a List<Integer> and work with them, e.g.:

      List<Integer> values = Arrays.stream(arr)
          .mapToInt(Integer::parseInt)
          .boxed()
          .toList();
      
    • 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).