Leveraging ChatGPT - GenAI as a Microsoft Data Expert

Speaker

Prerita Agarwal

Data Specialist @

23 Jul, 2024 @ 01:30 PM

Introduction

Hello Coders! In this blog, we will discuss the min and max functions in C++. We will first discuss when min and max functions can be used. Followed by the parameters and return value. We will also discuss the complexity and example of these functions. We hope that you are excited to learn about the min and max functions in C++.

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.

Get the tech career you deserve, faster!

Connect with our expert counsellors to understand how to hack your way to success

User rating 4.7/5

1:1 doubt support

95% placement record

Akash Pal

Senior Software Engineer

326% Hike After Job Bootcamp

Himanshu Gusain

Programmer Analyst

32 LPA After Job Bootcamp

After Job Bootcamp

Parameters

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

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.

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

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;
}

Output

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;
}

Output

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;
}

Output

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.

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.