Count number of unique Triangles using STL | Set 1 (Using set) - GeeksforGeeks

19 Mar, 2022
We are given n triangles along with length of their three sides as a,b,c. Now we need to count 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.Example:  
Input: arr[] = {{1, 2, 2},
                {4, 5, 6}, 
                {4, 5, 6}    
Output: 2

Input: arr[] = {{4, 5, 6}, {6, 5, 4},
                {1, 2, 2}, {8, 9, 12}
Output: 3
We strongly recommend you to minimize your browser and try this yourself first.Since we are asked to find number of “unique” triangles, we can use either set or unordered_set. In this post, set based approach is discussed.How to store three sides as an element in the container? We use STL pair to store all the three sides together as  
pair <int, pair<int, int> >
We one by one insert all triangles into the set. But the problem with this approach is that a triangle with sides as {4, 5, 6} is different from a triangle with sides {5, 4, 6} although they refer to a same triangle.In order to handle such cases, we store sides in sorted order (on the basis of length of sides), here sorting won’t be an issue since we have only 3 sides and we can sort them in constant time. For example {5, 4, 6} is inserted into set as {4, 5, 6}Note : We can make pair either by make_pair(a,b) or we can simply use {a, b}.Below is C++ implementation of the above idea: 
CPP
// A C++ program to find number of unique Triangles#include <bits/stdc++.h>using namespace std; // Creating shortcut for an integer pair.typedef pair<int, int> iPair; // A structure to represent a Triangle with// three sides as a, b, cstruct Triangle{    int a, b, c;}; // A function to sort three numbers a, b and c.// This function makes 'a' smallest, 'b' middle// and 'c' largest (Note that a, b and c are passed// by reference)void sort3(int &a, int &b, int &c){    vector<int> arr({a, b, c});    sort(arr.begin(), arr.end());    a = arr[0]; b = arr[1]; c = arr[2];} // Function returns the number of unique Trianglesint countUniqueTriangles(struct Triangle arr[],                           int n){    // A set which consists of unique Triangles    set < pair< int, iPair > > s;     // Insert all triangles one by one    for (int i=0; i<n; i++)    {        // Find three sides and sort them        int a = arr[i].a, b = arr[i].b, c = arr[i].c;        sort3(a, b, c);         // Insert a triangle into the set        s.insert({a, {b, c}});    }     // Return set size    return s.size();} // Driver program to test above functionint main(){    // An array of structure to store sides of 6 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);     cout << "Number of Unique Triangles are "         << countUniqueTriangles(arr, n);    return 0;}
Output:  
Number of Unique Triangles are 4
The time complexity of above solution is O(n Log n) as set requires O(Log n) time for insertion.We can improve time complexity to O(n) using unordered_set. But using unordered_set requires writing of a hash function as hash function is not defined in library for pairs. 
YouTubeGeeksforGeeks502K subscribersCount number of unique Triangles using STL | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 2:09•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=326XsTDFNEw" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
Related Article: Number of Possible TrianglesThis article is contributed by Chirag Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above 
Vik_24
atkvishnu
STL
triangle
C Language
C++
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Left Shift and Right Shift Operators in C/C++
fork() in C
Command line arguments in C/C++
Function Pointer in C
Substring in C++
Vector in C++ STL
Initialize a vector in C++ (6 different ways)
Inheritance in C++
Map in C++ Standard Template Library (STL)
C++ Classes and Objects