Given a Set, the task is to find the maximum and minimum element of this set in C++ STL. Examples:
Input: set={1, 6, 15, 10, 5}
Output: max = 15, min = 1
Input: set={10, 20, 30, 40, 50, 60}
Output: max = 60, min = 10
- Using set.begin() and set.end() methods Approach: Elements in a set are stored in sorted order. So the minimum element of the set will reside in the first element and the maximum element in the last element. Therefore, this first and last element can be fetched with the help of set.begin() and set.end() methods respectively. Program:
CPP
#include <bits/stdc++.h>
using namespace std;
void printSet(set< int > my_set)
{
cout << "Set: " ;
for ( auto i : my_set)
cout << i << " " ;
cout << '\n' ;
}
int findMax(set< int > my_set)
{
int max_element;
if (!my_set.empty())
max_element = *(my_set.rbegin());
return max_element;
}
int findMin(set< int > my_set)
{
int min_element;
if (!my_set.empty())
min_element = *my_set.begin();
return min_element;
}
int main()
{
set< int > my_set;
my_set.insert(1);
my_set.insert(6);
my_set.insert(15);
my_set.insert(10);
my_set.insert(5);
printSet(my_set);
cout << "Minimum element: "
<< findMin(my_set)
<< endl;
cout << "Maximum element: "
<< findMax(my_set)
<< endl;
}
|
Output:
Set: 1 5 6 10 15
Minimum element: 1
Maximum element: 15
Time Complexity: O(n)
Auxiliary Space: O(n)
- Using set.rbegin() and set.rend() methods Approach: Elements in a set are stored in sorted order. So the minimum element of the set will reside in the first element and the maximum element in the last element. Therefore, this first and last element can be fetched with the help of set.rend() and set.rbegin() methods respectively. Program:
CPP
#include <bits/stdc++.h>
using namespace std;
void printSet(set< int > my_set)
{
cout << "Set: " ;
for ( auto i : my_set)
cout << i << " " ;
cout << '\n' ;
}
int findMax(set< int > my_set)
{
int max_element;
if (!my_set.empty())
max_element = *my_set.rbegin();
return max_element;
}
int findMin(set< int > my_set)
{
int min_element;
if (!my_set.empty())
min_element = *(--my_set.rend());
return min_element;
}
int main()
{
set< int > my_set;
my_set.insert(1);
my_set.insert(6);
my_set.insert(15);
my_set.insert(10);
my_set.insert(5);
printSet(my_set);
cout << "Minimum element: "
<< findMin(my_set)
<< endl;
cout << "Maximum element: "
<< findMax(my_set)
<< endl;
}
|
Output:
Set: 1 5 6 10 15
Minimum element: 1
Maximum element: 15
Time Complexity: O(n)
Auxiliary Space: O(n)
Time Complexity: Both the methods are of constant time complexity. In set the maximum element is stored at last so we can return the last element using rbegin() method in O(1) time. Similarly, for minimum element using begin() method in O(1) time.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
21 Mar, 2023
Like Article
Save Article