- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Find number of unique triangles among given N triangles in C++

In this problem, we are given three arrays s1[] , s2[] and s3[] of size N, denoting N triangles. Our task is to find the number of unique triangles among given N triangles.

For a triangle to be unique, all its side should be unique i.e. no other triangle should have the same sides.

**Let’s take an example to understand the problem,**

## Input

s1[] = {1, 5, 3} s2[] = {2, 3, 2} s3[] = {4, 2, 5}

## Output

1

## Explanation

Triangle with sides 1 2 4 is unique.

## Solution Approach

A simple solution to the problem is by counting the number of triangles which are unique.

For this, we will first sort each triangle’s side and then store in a map, if it’s values are unique increase the count.

**Program to illustrate the working of our solution,**

## Example

#include <bits/stdc++.h> using namespace std; int countUniqueTriangle(int a[], int b[], int c[], int n) { vector<int> triSides[n]; map<vector<int>, int> m; for (int i = 0; i < n; i++) { triSides[i].push_back(a[i]); triSides[i].push_back(b[i]); triSides[i].push_back(c[i]); sort(triSides[i].begin(), triSides[i].end()); m[triSides[i]] = m[triSides[i]] + 1; } map<vector<int>, int>::iterator itr; int uniqueTriCount = 0; for (itr = m.begin(); itr != m.end(); itr++) { if (itr->second == 1) if (itr->second == 1) uniqueTriCount++; } return uniqueTriCount; } int main() { int s1[] = { 1, 5 ,3 }; int s2[] = { 2, 3, 2 }; int s3[] = { 4, 2, 5 }; int N = sizeof(s1) / sizeof(s1); cout<<"The number of unique triangles is "<<countUniqueTriangle(s1, s2, s3, N); return 0; }

## Output

The number of unique triangles is 1

- Related Articles
- Find the Number of Triangles After N Moves using C++
- Count number of right triangles possible with a given perimeter in C++
- Count the number of possible triangles in C++
- Count of triangles with total n points with m collinear in C++
- Area of the circumcircle of any triangles with sides given in C++
- C++ program to find the Area of the circumcircle of any triangles with sides given?
- C++ program to find the number of triangles amongst horizontal and vertical line segments
- Find the area of the following triangles."
- Find the Number of Triangles Formed from a Set of Points on Three Lines using C++
- Find the number of triangles in a heptagon if drawn from a single vertex.
- Find the area of each of the following triangles:"
- The sides of certain triangles are given below. Determine which of them are right triangles. (i) $a = 7 cm, b = 24 cm$ and $c = 25 cm$
- The sides of certain triangles are given below. Determine which of them are right triangles.(ii) $a = 9 cm, b = 16 cm$ and $c = 18 cm$
- The sides of certain triangles are given below. Determine which of them are right triangles.(iii) $a = 1.6 cm, b = 3.8 cm$ and $c = 4 cm$
- The sides of certain triangles are given below. Determine which of them are right triangles.(iv) $a = 8 cm, b = 10 cm$ and $c = 6 cm$

Advertisements