Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
The min and max functions in C++ are utility functions used to determine the smallest and largest values, respectively, from a given set of elements. These functions are part of the C++ Standard Library and can be applied to various data types, including integers, floating-point numbers, and custom objects. The min function returns the smaller of two values, while the max function returns the larger.
Both min and max functions in C++ are present in the <algorithm.h> library. Before coding must include this library in your code. So, what are we waiting for? Let's dig deep into the brief discussion.
The max function returns the larger between two numbers. In contrast, the min function returns the smaller between two numbers. If both numbers are equal, then return the first one. For this type of comparison, the max function uses the '<' operator and the min function uses the '>' operator.
From a given list, the max function returns the larger among all numbers. The min function returns the smaller among all numbers. If there is more than one number, it returns the first one. Comp (compare) binary function is used for this comparison.
Comp is a user-defined function that accepts two arguments and returns true if these arguments are in order. Otherwise, it returns false.
template <class T> const T&max( { list of elements }, [](const T& a, const T& b)
{
return a < b;
})
In the above code, we have created a template class T. We are given a list of elements and the comp function (return a < b) as an argument to the max function. Similarly, we can also create a “comp function” for min.
Parameters of C++ Min() and max() functions
Min and max functions in C++ accept three parameters:
First-value: The first value you want to compare.
Second-value: The second value you want to compare.
Comparator(Optional): A function that returns true if values are in order of the function and false otherwise. We need to pass the comp function as 3rd parameter when required.
Note: Both the first and second values must be of the same type.
Complexity
Let us look at the time and space complexity of the problem.
Time Complexity
The time complexity for the min and max functions in C++ will be as follows:
If we want to compare 'N' elements, then the complexity is N-1. Each element is compared with every other element so we have to traverse N-1 elements.
For the comparison of two numbers, complexity is constant. The complexity of the comparison of a list is linear.
Space Complexity
Space is required to store a list of ‘N’ elements. So, space complexity is O(N).
Maximum of 10 and 40 = 40
Minimum of 10 and 40 = 10
Maximum of 'C' and 'N' = N
Minimum of 'C' and 'N' = C
Maximum of 5.5 and 8.9 = 8.9
Minimum of 5.5 and 8.9 = 5.5
Maximum of 'Coding' and 'Ninjas' = Coding
Minimum of 'Coding' and 'Ninjas' = Ninjas
In this code,
We included the required header files.
Called min and max functions in C++ for two integer numbers.
Called functions for character values.
Called functions for floating point numbers.
Called the function for two given strings.
Example 2
Let’s see an example for comparison of a list of elements.
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
// Create comparator function
cout << "Maximum of given list : ";
cout << max( {1,5,3,4,5,1,5},
[](const int& a, const int& b)
{
return a < b;
});
return 0;
}
You can also try this code with Online C++ Compiler
Let's see another example of using the comparator's min and max functions in C++.
#include<iostream>
#include<algorithm>
using namespace std;
// Defining comparator for max
bool comp_max(int a, int b)
{
return (a < b);
}
// Defining comparator for min
bool comp_min(int a, int b)
{
return (a > b);
}
int main()
{
cout<<"Maximum of 10 and 40 = " << max(10, 40, comp_max)<< "\n";
cout<<"Minimum of 50 and 90 = " << min(50, 90, comp_min);
return 0;
}
You can also try this code with Online C++ Compiler
Max and min are predefined functions that return the largest and smallest values from the given values.
Which library is required for min and max functions in C++?
Both min and max functions are included in the 'algorithm' library.
What is the comparator function?
A comparator function is a binary function that returns true if values are in the order of the function and false otherwise.
What is the time complexity for the comparison?
For the comparison of two numbers, the time complexity is O(1), and for a list of numbers containing 'N' elements, the time complexity is O(N-1) = O(N).
How many parameters do min and max functions accept?
The min and max function in C++ accepts three parameters. The first value to compare, the second value to compare, and the comparator function(optional).
Conclusion
So, in this blog, we discussed the min and max functions in C++, their examples, complexity, return value, and parameters to be passed. We hope that now you have a clear understanding of the topic and are ready to give it an attempt yourself!
If you found this one interesting, explore our other related blogs as well:
To learn more about Data Structures and Algorithms, you can enroll in our DSA in C++ Course. Do upvote our blog to assist other ninjas in their development.