Table of contents
1.
Introduction
2.
Uses of Min() and Max() in C++
3.
Parameters of C++ Min() and max() functions 
4.
Complexity
4.1.
Time Complexity
4.2.
Space Complexity
5.
Return Values of C++ Min() and max() functions 
6.
Examples of  Min() and max() in C++
6.1.
Example 1
6.1.1.
Output
6.2.
Example 2
6.2.1.
Output
6.3.
Example 3
6.3.1.
Output
7.
Frequently Asked Questions 
7.1.
What is C++ max() and min() function?
7.2.
Which library is required for min and max functions in C++?
7.3.
What is the comparator function?
7.4.
What is the time complexity for the comparison?
7.5.
How many parameters do min and max functions accept?
8.
Conclusion
Last Updated: Oct 7, 2024
Easy

Min() and Max() function in C++

Author Ayushi Goyal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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.

introduction

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.

Also see, Literals in C, Fibonacci Series in C++

Uses of Min() and Max() in C++

  • 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. 
uses
  • 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:

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:

time complexity

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).     

Read More - Time Complexity of Sorting Algorithms

Return Values of C++ Min() and max() functions 

For two numbers “A” and “B”, 

  • The Max function returns the maximum from A and B.
  • The Min function returns the minimum from A and B. 
  • If both numbers are equal, the min and max functions in C++ returns A. 
return value

For a list of elements

  • The Max function returns the largest among the list. 
  • The Min function returns the smallest among the list. 
  • If several elements are equivalent, then it returns left most element. 

Examples of  Min() and max() in C++

Let us look at a few examples of min and max functions below:

Example 1

#include <iostream>       
#include <algorithm>      
using namespace std;
  
int main () 
{      
    // For integer values
    cout << "Maximum of 10 and 40 = " << max(10, 40) << '\n';  
    cout << "Minimum of 10 and 40 = " << min(10, 40) << '\n';  
    
    // For chrachter values
    cout << "\nMaximum of 'C' and 'N' = " << max('C','N') << '\n';  
    cout << "Minimum of 'C' and 'N' = " << min('C','N') << '\n';
    
    // For floating numbers 
    cout << "\nMaximum of 5.5 and 8.9 = " << max(5.5, 8.9) << '\n';
    cout << "Minimum of 5.5 and 8.9 = " << min(5.5, 8.9) << '\n'; 
    
    // For string 
    cout << "\nMaximum of 'Coding' and 'Ninjas' = " << max("Coding", "Ninjas") << '\n';
    cout << "Minimum of 'Coding' and 'Ninjas' = " << min("Coding", "Ninjas") << '\n';
    
    return 0;  
}  
You can also try this code with Online C++ Compiler
Run Code

 

Output

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
Run Code

 

Output

Maximum of given list : 5

Example 3

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
Run Code

 

Output

Maximum of 10 and 40 = 40 
Minimum of 50 and 90 = 90

 

In this code, 

  • We included the required header files.
  • Declared a function 'comp_max' as comparator for max function. 
  • Declared a function 'comp_min' as a comparator for the min function. 
  • Called the max function and printed the result
  • Called the min function and printed the result.

You can try and compile with the help of online c++ compiler.

Check out this problem - Next Smaller Element

Must Read Lower Bound in C++

Frequently Asked Questions 

What is C++ max() and min() function?

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. 

Live masterclass