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: 3
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:
C++
#include <bits/stdc++.h>
using namespace std;
struct Triangle {
int a, b, c;
public :
bool operator==( const Triangle& t) const ;
};
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 (cnt == 3) {
return false ;
}
return true ;
}
int countUniqueTriangles( struct Triangle arr[],
int n)
{
int uni = 0;
for ( int i = 0; i < n - 1; i++) {
int cnt = 0;
for ( int j = i; j < n - 1; j++) {
if (arr[i] == arr[j + 1])
cnt++;
}
if (cnt == n - 1 - i)
uni++;
}
return uni + 1;
}
int main()
{
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);
cout << countUniqueTriangles(arr, n);
return 0;
}
|
Java
import java.util.*;
class Triangle {
int a, b, c;
public Triangle( int a, int b, int c)
{
this .a = a;
this .b = b;
this .c = c;
}
@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 (cnt == 3 ) {
return false ;
}
return true ;
}
}
public class Main {
public static int countUniqueTriangles(Triangle[] arr)
{
int n = arr.length;
int uni = 0 ;
for ( int i = 0 ; i < n - 1 ; i++) {
int cnt = 0 ;
for ( int j = i; j < n - 1 ; j++) {
if (arr[i].equals(arr[j + 1 ]))
cnt++;
}
if (cnt == n - 1 - i)
uni++;
}
return uni + 1 ;
}
public static void main(String[] args)
{
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 )
};
System.out.println(countUniqueTriangles(arr));
}
}
|
Python3
class Triangle:
def __init__( self , a, b, c):
self .a = a
self .b = b
self .c = c
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 cnt = = 3 :
return False
return True
def countUniqueTriangles(arr, n):
uni = 0 ;
for i in range (n - 1 ):
cnt = 0 ;
for j in range (i, n - 1 ):
if (arr[i] = = arr[j + 1 ]):
cnt + = 1
if (cnt = = n - 1 - i):
uni + = 1 ;
return uni + 1 ;
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)
print (countUniqueTriangles(arr, n))
|
C#
using System;
public struct Triangle
{
public int a, b, c;
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 (cnt == 3)
{
return false ;
}
return true ;
}
public static bool operator !=(Triangle t1, Triangle t2)
{
return !(t1 == t2);
}
}
public class Program
{
public static int CountUniqueTriangles(Triangle[] arr, int n)
{
int uni = 0;
for ( int i = 0; i < n - 1; i++)
{
int cnt = 0;
for ( int j = i; j < n - 1; j++)
{
if (arr[i] == arr[j + 1])
cnt++;
}
if (cnt == n - 1 - i)
uni++;
}
return uni + 1;
}
public static void Main()
{
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;
Console.WriteLine(CountUniqueTriangles(arr, n));
}
}
|
Javascript
class Triangle {
constructor(a, b, c) {
this .a = a;
this .b = b;
this .c = c;
}
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 (cnt === 3) {
return false ;
}
return true ;
}
}
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++) {
if (arr[i].equals(arr[j + 1])) {
cnt++;
}
}
if (cnt === n - 1 - i) {
uni++;
}
}
return uni + 1;
}
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;
console.log(countUniqueTriangles(arr, n));
|
Time Complexity: O(N) Auxiliary Space: O(1)
Last Updated :
02 Nov, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...