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.