VOOZH about

URL: https://www.geeksforgeeks.org/dsa/count-number-of-unique-triangles-using-operator-overloading/

⇱ Count number of Unique Triangles using Operator overloading - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Count number of Unique Triangles using Operator overloading

Last Updated : 15 Jul, 2025

Given N triangles along with the length of their three sides as a, b and c. The task is to count the number of unique triangles out of these N given triangles. Two triangles are different from one another if they have at least one of the sides different. 

Examples:

Input: arr[] = {{3, 1, 2}, {2, 1, 4}, {4, 5, 6}, {6, 5, 4}, {4, 5, 6}, {5, 4, 6}}; 
Output:

Input: arr[] = {{4, 5, 6}, {6, 5, 4}, {1, 2, 2}, {8, 9, 12}}; 
Output: 3

This problem has been solved using ordered set of STL in the previous post. Approach: We will be discussing the operator overloading based approach to solve this problem where we are going to overload the relational operator (==) of our class.

  • Since any two sets of sides of a triangle, say {4, 5, 6}, {6, 5, 4}, are said to be equal if each element in one set corresponds to the elements in the other. So we will be checking each element of one set with the elements of the other set and keep a count of it. If the count will be the same, both the sets can simply be considered as equal. Now we have simply compared the sets using the relational operator to find the unique number of sets.
  • To get the number of unique sets, we can follow an approach of comparing the current set's uniqueness with the sets ahead of it. So, if there will be k sets of the same type, only the last set would be considered to be unique.

Below is C++ implementation of the above approach: 


Output
4

Time Complexity:O(N)Auxiliary Space:O(1)

Comment