Open In App

Count number of Unique Triangles using Operator overloading

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.

Below is C++ implementation of the above approach: 




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Structure to represent a Triangle
// with three sides as a, b, c
struct Triangle {
    int a, b, c;
 
public:
    bool operator==(const Triangle& t) const;
};
 
// Function to overload relational
// operator (==)
bool Triangle::operator==(const Triangle& t) const
{
    int cnt = 0;
    if ((this->a == t.a)
        || (this->a == t.b)
        || (this->a == t.c)) {
        cnt++;
    }
    if ((this->b == t.a)
        || (this->b == t.b)
        || (this->b == t.c)) {
        cnt++;
    }
    if ((this->c == t.a)
        || (this->c == t.b)
        || (this->c == t.c)) {
        cnt++;
    }
 
    // If all the three elements a, b, c
    // are same, triangle is not unique
    if (cnt == 3) {
        return false;
    }
 
    // For unique triangle return true
    return true;
}
 
// Function returns the number
// of unique Triangles
int countUniqueTriangles(struct Triangle arr[],
                         int n)
{
 
    // Unique sets
    int uni = 0;
 
    for (int i = 0; i < n - 1; i++) {
 
        // Check on uniqueness for a
        // particular set w.r.t others
        int cnt = 0;
 
        for (int j = i; j < n - 1; j++) {
 
            // Checks if two triangles
            // are different
            if (arr[i] == arr[j + 1])
                cnt++;
        }
 
        // If count of unique triangles
        // is same as the number of remaining
        // triangles then, increment count
        if (cnt == n - 1 - i)
            uni++;
    }
 
    // Since last element that
    // remains will be unique only
    return uni + 1;
}
 
// Driver Code
int main()
{
    // An array of structure to
    // store sides of Triangles
    struct Triangle arr[] = {
        { 3, 2, 2 }, { 3, 4, 5 }, { 1, 2, 2 },
        { 2, 2, 3 }, { 5, 4, 3 }, { 6, 4, 5 }
    };
 
    int n = sizeof(arr) / sizeof(Triangle);
 
    // Function Call
    cout << countUniqueTriangles(arr, n);
    return 0;
}




// Java program for the above approach
import java.util.*;
 
// Class to represent a Triangle
// with three sides as a, b, c
class Triangle {
    int a, b, c;
    // Constructor
    public Triangle(int a, int b, int c)
    {
        this.a = a;
        this.b = b;
        this.c = c;
    }
 
    // Function to check if two triangles are equal
    @Override public boolean equals(Object obj)
    {
        if (obj == this)
            return true;
 
        if (!(obj instanceof Triangle))
            return false;
 
        Triangle t = (Triangle)obj;
 
        int cnt = 0;
        if ((this.a == t.a) || (this.a == t.b)
            || (this.a == t.c)) {
            cnt++;
        }
        if ((this.b == t.a) || (this.b == t.b)
            || (this.b == t.c)) {
            cnt++;
        }
        if ((this.c == t.a) || (this.c == t.b)
            || (this.c == t.c)) {
            cnt++;
        }
 
        // If all the three elements a, b, c
        // are same, triangle is not unique
        if (cnt == 3) {
            return false;
        }
 
        // For unique triangle return true
        return true;
    }
}
 
// Driver Class
public class Main { // Function to count the number of
                    // unique triangles
    public static int countUniqueTriangles(Triangle[] arr)
    {
        int n = arr.length;
 
        // Unique sets
        int uni = 0;
 
        for (int i = 0; i < n - 1; i++) {
 
            // Check on uniqueness for a
            // particular set w.r.t others
            int cnt = 0;
 
            for (int j = i; j < n - 1; j++) {
 
                // Checks if two triangles
                // are different
                if (arr[i].equals(arr[j + 1]))
                    cnt++;
            }
 
            // If count of unique triangles
            // is same as the number of remaining
            // triangles then, increment count
            if (cnt == n - 1 - i)
                uni++;
        }
 
        // Since last element that
        // remains will be unique only
        return uni + 1;
    }
 
    // Main Function
    public static void main(String[] args)
    {
        // An array of objects of Triangle class to
        // store sides of Triangles
        Triangle[] arr = {
            new Triangle(3, 2, 2), new Triangle(3, 4, 5),
            new Triangle(1, 2, 2), new Triangle(2, 2, 3),
            new Triangle(5, 4, 3), new Triangle(6, 4, 5)
        };
 
        // Function Call
        System.out.println(countUniqueTriangles(arr));
    }
}




# Python3 program for the above approach
 
# Structure to represent a Triangle
# with three sides as a, b, c
class Triangle:
     
    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c
     
    # Function to overload  relational
    # operator
    def __eq__(self, t):
        cnt = 0
        if self.a in [t.a, t.b, t.c]:
            cnt += 1
        if self.b in [t.a, t.b, t.c]:
            cnt += 1
        if self.c in [t.a, t.b, t.c]:
            cnt += 1
         
        # If all the three elements a, b, c
        # are same, triangle is not unique
        if cnt == 3:
            return False
         
        # For unique triangle, return True
        return True
 
 
# Function returns the number
# of unique Triangles
def countUniqueTriangles(arr, n):
 
    # Unique sets
    uni = 0;
 
    for i in range(n - 1):
 
        # Check on uniqueness for a
        # particular set w.r.t others
        cnt = 0;
 
        for j in range(i, n - 1):
 
            # Checks if two triangles
            # are different
            if (arr[i] == arr[j + 1]):
                cnt += 1
         
 
        # If count of unique triangles
        # is same as the number of remaining
        # triangles then, increment count
        if (cnt == n - 1 - i):
            uni+=1;
     
 
    # Since last element that
    # remains will be unique only
    return uni + 1;
 
# Driver Code
 
# An array of structure to
# store sides of Triangles
arr = [ Triangle(3, 2, 2), Triangle(3, 4, 5), Triangle(1, 2, 2), Triangle(2, 2, 3), Triangle(5, 4, 3), Triangle(6, 4, 5) ]
 
n = len(arr)
 
# Function Call
print(countUniqueTriangles(arr, n))
 
# This code is contributed by phasing17.




using System;
 
// Structure to represent a Triangle
// with three sides as a, b, c
public struct Triangle
{
    public int a, b, c;
 
    // Function to overload relational
    // operator (==)
    public static bool operator ==(Triangle t1, Triangle t2)
    {
        int cnt = 0;
        if ((t1.a == t2.a)
            || (t1.a == t2.b)
            || (t1.a == t2.c))
        {
            cnt++;
        }
        if ((t1.b == t2.a)
            || (t1.b == t2.b)
            || (t1.b == t2.c))
        {
            cnt++;
        }
        if ((t1.c == t2.a)
            || (t1.c == t2.b)
            || (t1.c == t2.c))
        {
            cnt++;
        }
 
        // If all the three elements a, b, c
        // are same, triangle is not unique
        if (cnt == 3)
        {
            return false;
        }
 
        // For unique triangle return true
        return true;
    }
 
    // Function to overload relational
    // operator (!=)
    public static bool operator !=(Triangle t1, Triangle t2)
    {
        return !(t1 == t2);
    }
}
 
// Class to hold the main program
public class Program
{
    // Function returns the number
    // of unique Triangles
    public static int CountUniqueTriangles(Triangle[] arr, int n)
    {
        // Unique sets
        int uni = 0;
 
        for (int i = 0; i < n - 1; i++)
        {
            // Check on uniqueness for a
            // particular set w.r.t others
            int cnt = 0;
 
            for (int j = i; j < n - 1; j++)
            {
                // Checks if two triangles
                // are different
                if (arr[i] == arr[j + 1])
                    cnt++;
            }
 
            // If count of unique triangles
            // is same as the number of remaining
            // triangles then, increment count
            if (cnt == n - 1 - i)
                uni++;
        }
 
        // Since last element that
        // remains will be unique only
        return uni + 1;
    }
 
    // Main function
    public static void Main()
    {
        // An array of structure to
        // store sides of Triangles
        Triangle[] arr = {
            new Triangle { a = 3, b = 2, c = 2 },
            new Triangle { a = 3, b = 4, c = 5 },
            new Triangle { a = 1, b = 2, c = 2 },
            new Triangle { a = 2, b = 2, c = 3 },
            new Triangle { a = 5, b = 4, c = 3 },
            new Triangle { a = 6, b = 4, c = 5 }
        };
 
        int n = arr.Length;
 
        // Function Call
        Console.WriteLine(CountUniqueTriangles(arr, n));
    }
}
// This code is contributed by divyansh2212




// Class to represent a Triangle
class Triangle {
  constructor(a, b, c) {
    this.a = a;
    this.b = b;
    this.c = c;
  }
 
  // Function to overload relational operator
  equals(t) {
    let cnt = 0;
    if ([t.a, t.b, t.c].includes(this.a)) {
      cnt++;
    }
    if ([t.a, t.b, t.c].includes(this.b)) {
      cnt++;
    }
    if ([t.a, t.b, t.c].includes(this.c)) {
      cnt++;
    }
 
    // If all the three elements a, b, c
    // are same, triangle is not unique
    if (cnt === 3) {
      return false;
    }
 
    // For unique triangle, return True
    return true;
  }
}
 
// Function to count the number of unique triangles
function countUniqueTriangles(arr, n) {
  let uni = 0;
 
  for (let i = 0; i < n - 1; i++) {
    let cnt = 0;
 
    for (let j = i; j < n - 1; j++) {
      // Checks if two triangles are different
      if (arr[i].equals(arr[j + 1])) {
        cnt++;
      }
    }
 
    // If count of unique triangles is same as the number of remaining triangles
    // then, increment count
    if (cnt === n - 1 - i) {
      uni++;
    }
  }
 
  // Since the last element that remains will be unique only
  return uni + 1;
}
 
// Driver Code
 
// An array of structure to store sides of Triangles
const arr = [
  new Triangle(3, 2, 2),
  new Triangle(3, 4, 5),
  new Triangle(1, 2, 2),
  new Triangle(2, 2, 3),
  new Triangle(5, 4, 3),
  new Triangle(6, 4, 5),
];
 
const n = arr.length;
 
// Function Call
console.log(countUniqueTriangles(arr, n));

Output
4

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


Article Tags :