-2

question i am trying to solve.

You are given n triangles.

You are required to find how many triangles are unique out of given triangles. For each triangle you are given three integers a,b,c , the sides of a triangle.

sample input: 5

7 6 5

5 7 6

8 2 9

2 3 4

2 4 3

here is my code:


class TestClass {
    public static void main(String args[]) throws Exception {

        Scanner scanner = new Scanner(System.in);
        int testCases = scanner.nextInt();
        int arr[][]=new int[testCases][3];
        int count=0;
        for (int i = 0; i < testCases; i++) {
            for (int j=0;j<3;j++){
                arr[i][j]=scanner.nextInt();
            }
        }
        int result[] =new int[testCases];
        for (int i = 0; i < testCases; i++) {
            for (int j = 0; j < 3; j++) {
                result[i] = arr[i][j]+arr[i][j+1];       //possible error
            }
        }
        for (int i=0;i<testCases;i++){
            for (int j=i+1;j<testCases;j++) {           //possible error
                if (result[i]!=result[j]){
                    count++;
                }
            }
        }
        System.out.println(count);

    }
}

error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
    at P1.TestClass.main(Solution.java:21)

how to correct the loops so as to not get the errors(note there maybe other errors than the one's i have highlighted) also some better ways of solving this problem are appreciated.

NoviceCoder
  • 83
  • 1
  • 7

2 Answers2

0

Your program has an ArrayIndexOutOfBoundException in the line result[i] = arr[i][j]+arr[i][j+1];. And I am not sure your second set of nested loops achieve what you want (Summing the triangles). Here is something that can work.

class TestClass {
  public static void main(String args[]) throws Exception {
    Scanner scanner = new Scanner(System.in);
    int testCases = scanner.nextInt();
    int arr[][]=new int[testCases][3];
    int count=0;
    for (int i = 0; i < testCases; i++) {
        for (int j=0;j<3;j++){
            arr[i][j]=scanner.nextInt();
        }
    }

    //This section sums each set of three
    int result[] =new int[testCases];
    for (int i = 0; i < testCases; i++) {
        for (int j = 0; j < 3; j++) {
            result[i] += arr[i][j];       
        }
    }
   

    //This section counts values that are not duplicated
    for (int i=0;i<testCases;i++){
        boolean hasDuplicate = false;
        for (int j=0;j<testCases;j++) {           
            if (i == j) continue; //skip comparison of value against itself
            if (result[i]==result[j]){
                hasDuplicate = true; //duplicate found
            }
        }
        if (!hasDuplicate) count++;
    }
    System.out.println(count); //display number of unique entries
}
}
Kasalwe
  • 358
  • 2
  • 6
0

I don't want to be doing any homework for you, so I'll give you some pointers. Try not to have a look at a solution I have come up with below before trying it yourself again though.

  1. I'd use an ArrayList to store the test data given to you. They're really useful and Java has great support for them.
  2. result[i] = arr[i][j]+arr[i][j+1]; This breaks because j+1 will always be one over the final index of your array.
  3. You can sort strings alphabetically using Arrays.sort which will make comparing triangles much easier, as possible combinations will all end up the same.
  4. Collections.frequency will tell you how many times an element appears in your ArrayList.

My solution certainly isn't the best, and uses more advanced things as apposed to just arrays and booleans, but it works and produces the right answer at the end. It shows that you can solve problems in many different ways!

public static void main(String[] args) {
    ArrayList<String> triangleArray = new ArrayList<String>();
    Scanner scanner = new Scanner(System.in);
    int uniqueTriangles = 0;

    // Ask for input, and store in a string that remove all whitespace
    System.out.print("Enter triangle sides in format abc separated by a comma:");
    String input = scanner.nextLine().trim().replaceAll("\\s", "");
    triangleArray.addAll(Arrays.asList(input.split(",")));

    // For each item, check it is three characters, and if so, reorder them in 
    // ascending order i.e 726 => 267
    for (int i = 0; i < triangleArray.size(); i++) {
        if (triangleArray.get(i).length() != 3) {
            triangleArray.remove(i);
        }
            
        // Split the triangle string into a character array and sort 'alphabetically'
        char[] charArray = triangleArray.get(i).toCharArray();
        Arrays.sort(charArray);
        triangleArray.set(i, new String(charArray));
    }

    // Now go through them all again and see if any are unique
    for (String s : triangleArray) {
        if (Collections.frequency(triangleArray, s) < 2) {
            uniqueTriangles++;
        }
    }

    System.out.println(uniqueTriangles);
}
Rhys Wootton
  • 182
  • 8