Counting Triangles

	class Sides {
		int a;
		int b;
		int c;

		Sides(int a, int b, int c) {
			this.a = a;
			this.b = b;
			this.c = c;
		}

	}

	public int compare(Sides o1, Sides o2) {
		int[] o1Sides = new int[] { o1.a, o1.b, o1.c };
		Arrays.sort(o1Sides);
		int[] o2Sides = new int[] { o2.a, o2.b, o2.c };
		Arrays.sort(o2Sides);
		for (int i = 0; i < o1Sides.length; i++)
			if (o1Sides[i] != o2Sides[i])
				return -1;

		return 0;
	}

	public int countDistinctTriangles(ArrayList<Sides> arr) {
		if (arr.size() == 0)
			return 0;
		Collections.sort(arr, new Comparator<Sides>() {

			@Override
			public int compare(Sides o1, Sides o2) {
				int[] o1Sides = new int[] { o1.a, o1.b, o1.c };
				Arrays.sort(o1Sides);
				int[] o2Sides = new int[] { o2.a, o2.b, o2.c };
				Arrays.sort(o2Sides);
				for (int i = 0; i < o1Sides.length; i++)
					if (o1Sides[i] != o2Sides[i])
						return -1;

				return 0;
			}
		});
		int count = 1;
		for (int i = 1; i < arr.size(); i++) {
			if (compare(arr.get(i), arr.get(i - 1)) != 0)
				count++;
		}
		return count;
	}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s