Find Maximum and Minimum element in a Set in C++ STL - GeeksforGeeks

21 Jun, 2019
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() methodsApproach: 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:#include <bits/stdc++.h>using namespace std;  // Function to print the setvoid printSet(set<int> my_set){      // Print the set    cout << "Set: ";    for (auto i : my_set)        cout << i << " ";      cout << '\n';}  // Function to find the maximum elementint findMax(set<int> my_set){      // Get the maximum element    int max_element;    if (!my_set.empty())        max_element = *(my_set.rbegin());      // return the maximum element    return max_element;}  // Function to find the minimum elementint findMin(set<int> my_set){      // Get the minimum element    int min_element;    if (!my_set.empty())        min_element = *my_set.begin();      // return the minimum element    return min_element;}  int main(){      // Get the set    set<int> my_set;      // Add the elements in the set    my_set.insert(1);    my_set.insert(6);    my_set.insert(15);    my_set.insert(10);    my_set.insert(5);      // Print the set    printSet(my_set);      // Get the minimum element    cout << "Minimum element: "         << findMin(my_set)         << endl;      // Get the maximum element    cout << "Maximum element: "         << findMax(my_set)         << endl;}Output:Set: 1 5 6 10 15 
Minimum element: 1
Maximum element: 15

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:
#include <bits/stdc++.h>using namespace std;  // Function to print the setvoid printSet(set<int> my_set){      // Print the set    cout << "Set: ";    for (auto i : my_set)        cout << i << " ";      cout << '\n';}  // Function to find the maximum elementint findMax(set<int> my_set){      // Get the maximum element    int max_element;    if (!my_set.empty())        max_element = *(my_set.rbegin());      // return the maximum element    return max_element;}  // Function to find the minimum elementint findMin(set<int> my_set){      // Get the minimum element    int min_element;    if (!my_set.empty())        min_element = *my_set.begin();      // return the minimum element    return min_element;}  int main(){      // Get the set    set<int> my_set;      // Add the elements in the set    my_set.insert(1);    my_set.insert(6);    my_set.insert(15);    my_set.insert(10);    my_set.insert(5);      // Print the set    printSet(my_set);      // Get the minimum element    cout << "Minimum element: "         << findMin(my_set)         << endl;      // Get the maximum element    cout << "Maximum element: "         << findMax(my_set)         << endl;}
Set: 1 5 6 10 15 
Minimum element: 1
Maximum element: 15

Using set.rbegin() and set.rend() methodsApproach: 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:#include <bits/stdc++.h>using namespace std;  // Function to print the setvoid printSet(set<int> my_set){      // Print the set    cout << "Set: ";    for (auto i : my_set)        cout << i << " ";      cout << '\n';}  // Function to find the maximum elementint findMax(set<int> my_set){      // Get the maximum element    int max_element;    if (!my_set.empty())        max_element = *my_set.rbegin();      // return the maximum element    return max_element;}  // Function to find the minimum elementint findMin(set<int> my_set){      // Get the minimum element    int min_element;    if (!my_set.empty())        min_element = *(--my_set.rend());      // return the minimum element    return min_element;}  int main(){      // Get the set    set<int> my_set;      // Add the elements in the set    my_set.insert(1);    my_set.insert(6);    my_set.insert(15);    my_set.insert(10);    my_set.insert(5);      // Print the set    printSet(my_set);      // Get the minimum element    cout << "Minimum element: "         << findMin(my_set)         << endl;      // Get the maximum element    cout << "Maximum element: "         << findMax(my_set)         << endl;}Output:Set: 1 5 6 10 15 
Minimum element: 1
Maximum element: 15
My Personal Notes
arrow_drop_upSave
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:
#include <bits/stdc++.h>using namespace std;  // Function to print the setvoid printSet(set<int> my_set){      // Print the set    cout << "Set: ";    for (auto i : my_set)        cout << i << " ";      cout << '\n';}  // Function to find the maximum elementint findMax(set<int> my_set){      // Get the maximum element    int max_element;    if (!my_set.empty())        max_element = *my_set.rbegin();      // return the maximum element    return max_element;}  // Function to find the minimum elementint findMin(set<int> my_set){      // Get the minimum element    int min_element;    if (!my_set.empty())        min_element = *(--my_set.rend());      // return the minimum element    return min_element;}  int main(){      // Get the set    set<int> my_set;      // Add the elements in the set    my_set.insert(1);    my_set.insert(6);    my_set.insert(15);    my_set.insert(10);    my_set.insert(5);      // Print the set    printSet(my_set);      // Get the minimum element    cout << "Minimum element: "         << findMin(my_set)         << endl;      // Get the maximum element    cout << "Maximum element: "         << findMax(my_set)         << endl;}
Set: 1 5 6 10 15 
Minimum element: 1
Maximum element: 15

klakshmanan6499
cpp-set
STL
C++
C++ Programs
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Operator Overloading in C++
Polymorphism in C++
Friend class and function in C++
Sorting a vector in C++
std::string class in C++
Header files in C/C++ and its uses
Program to print ASCII Value of a character
C++ Program for QuickSort
How to return multiple values from a function in C or C++?
Sorting a Map by value in C++ STL