Find number of unique triangles among given N triangles
Last Updated :
08 Nov, 2022
Given three arrays a[], b[], and c[] of N elements representing the three sides of N triangles. The task is to find the number of triangles that are unique out of given triangles. A triangle is non-unique if all of its sides match with all the sides of some other triangle in length.
Examples:
Input: a[] = {1, 2}, b[] = {2, 3}, c[] = {3, 5}
Output: 2
Explanation:
The triangles have sides 1, 2, 3 and 2, 3, 5 respectively.
None of them have same sides. Thus both are unique.
Input: a[] = {7, 5, 8, 2, 2}, b[] = {6, 7, 2, 3, 4}, c[] = {5, 6, 9, 4, 3}
Output: 1
Only triangle with sides 8, 2 and 9 is unique.
Approach: The idea is, for each triangle, sort all of its sides and then store it in a map, if all those three sides are already present in the map then increase the frequency by 1, else its frequency will be 1. The count of elements of the map which have frequency 1 in the end will be the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int UniqueTriangles( int a[], int b[], int c[], int n)
{
vector< int > sides[n];
map<vector< int >, int > m;
for ( int i = 0; i < n; i++) {
sides[i].push_back(a[i]);
sides[i].push_back(b[i]);
sides[i].push_back(c[i]);
sort(sides[i].begin(), sides[i].end());
m[sides[i]] = m[sides[i]] + 1;
}
map<vector< int >, int >::iterator i;
int count = 0;
for (i = m.begin(); i != m.end(); i++) {
if (i->second == 1)
count++;
}
return count;
}
int main()
{
int a[] = { 7, 5, 8, 2, 2 };
int b[] = { 6, 7, 2, 3, 4 };
int c[] = { 5, 6, 9, 4, 3 };
int n = sizeof (a) / sizeof ( int );
cout << UniqueTriangles(a, b, c, n);
return 0;
}
|
Java
import java.util.*;
public class Solution
{
static int UniqueTriangles( int [] a, int [] b, int [] c,
int n)
{
String[][] sides = new String[n][ 3 ];
HashMap<String, Integer> m = new HashMap<>();
for (var i = 0 ; i < n; i++) {
String[] arr
= { a[i] + "" , b[i] + "" , c[i] + "" };
Arrays.sort(arr);
sides[i] = arr;
String key = String.join( "," , sides[i]);
if (!m.containsKey(key))
m.put(key, 0 );
m.put(key, m.getOrDefault(key, m.get(key)) + 1 );
}
int count = 0 ;
for (String s : m.keySet())
if (m.get(s) == 1 )
count++;
return count;
}
public static void main(String[] args)
{
int [] a = { 7 , 5 , 8 , 2 , 2 };
int [] b = { 6 , 7 , 2 , 3 , 4 };
int [] c = { 5 , 6 , 9 , 4 , 3 };
int n = a.length;
System.out.println(UniqueTriangles(a, b, c, n));
}
}
|
Python3
from collections import defaultdict
def UniqueTriangles(a, b, c, n):
sides = [ None for i in range (n)]
m = defaultdict( lambda : 0 )
for i in range ( 0 , n):
sides[i] = (a[i], b[i], c[i])
sides[i] = tuple ( sorted (sides[i]))
m[sides[i]] + = 1
count = 0
for i in m:
if m[i] = = 1 :
count + = 1
return count
if __name__ = = "__main__" :
a = [ 7 , 5 , 8 , 2 , 2 ]
b = [ 6 , 7 , 2 , 3 , 4 ]
c = [ 5 , 6 , 9 , 4 , 3 ]
n = len (a)
print (UniqueTriangles(a, b, c, n))
|
Javascript
function UniqueTriangles(a, b, c, n)
{
let sides = new Array(n)
let m = {}
for ( var i = 0; i < n; i++)
{
let arr = [a[i], b[i], c[i]]
arr.sort()
sides[i] = arr
let key = sides[i].join( '#' )
if (!m.hasOwnProperty(key))
m[key] = 0
m[key] += 1
}
let count = 0
for (let [i, val] of Object.entries(m))
if (m[i] == 1)
count += 1
return count
}
let a = [7, 5, 8, 2, 2]
let b = [6, 7, 2, 3, 4]
let c = [5, 6, 9, 4, 3]
let n = a.length
console.log(UniqueTriangles(a, b, c, n))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int UniqueTriangles( int [] a, int [] b, int [] c,
int n)
{
int [][] sides = new int [n][];
Dictionary< string , int > m
= new Dictionary< string , int >();
for ( var i = 0; i < n; i++) {
int [] arr = { a[i], b[i], c[i] };
Array.Sort(arr);
sides[i] = arr;
string key = string .Join( "," , sides[i]);
if (!m.ContainsKey(key))
m[key] = 0;
m[key] += 1;
}
int count = 0;
foreach ( var entry in m)
if (entry.Value == 1) count++;
return count;
}
public static void Main( string [] args)
{
int [] a = { 7, 5, 8, 2, 2 };
int [] b = { 6, 7, 2, 3, 4 };
int [] c = { 5, 6, 9, 4, 3 };
int n = a.Length;
Console.WriteLine(UniqueTriangles(a, b, c, n));
}
}
|
Time Complexity : O(N * log(N))
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...