Find minimum/maximum using c++
Finding min or max is very useful operation to solve coding problem. In this post we will discuss how we can use c++ efficiently to find min/max on different types of input containers.
Min of two numbers
We know that two numbers can be easily be compared using <
sign therefore we can write code as below.
We can write our code as below.
int min(int a, int b) {
if(a < b) {
return a;
}
return b;
}
min/max standard library method
We can also use existing min
library method as below.
int c = min(a,b);
int d = max(a,b);
min_element/max_element standard library method
We can use existing standard library function min_element
or max_element
to find out min or max on any container which support iterator.
Following is function signature.
template <class ForwardIterator>
ForwardIterator min_element(ForwardIterator first, ForwardIterator last);
Parameters:
first
: An iterator pointing to the beginning of the range.last
: An iterator pointing to one past the end of the range.
Return value:
- An iterator pointing to the element with the minimum value in the specified range.
Algorithm:
- Iterates through the range, comparing each element with the current minimum.
- If an element is found to be smaller than the current minimum, it becomes the new minimum.
- Returns an iterator pointing to the final minimum element.
On array
Please note; min_element
returns iterator pointing to the min element. Therefore we need to apply *
operator dereferences the iterator.
int min (int input[], int size) {
auto min_itr= min_element(input, input + size);
return *min_itr;
}
int main() {
int nums[] = {5,8,1,4};
int size = sizeof(nums)/sizeof(nums[0]);
cout << min(nums, size);
}
On vector
Following is code for reference.
int min (vector<int> input) {
return *min_element(input.begin(), input.end());
}
Following is code to show the type of iterator.
int min (vector<int> input) {
vector<int>::iterator itr= min_element(input.begin(), input.end());
return *itr;
}
Following is code to store the address of min element.
int min (vector<int> input) {
int* ptr = &(*min_element(input.begin(), input.end()));
return *ptr;
}
Following is code to use the iterator to iterate rest of the array from the found min element.
vector<int>::iterator min_itr= min_element(input.begin(), input.end());
for (vector<int>::iterator itr = min_itr; itr < input.end(); itr++ ) {
cout << *itr << " ";
}
We can write same code with help of auto
as below.
auto min_itr= min_element(input.begin(), input.end());
for (auto itr = min_itr; itr < input.end(); itr++ ) {
cout << *itr << " ";
}
Following is code to find the index of the min element.
vector<int>::iterator min_itr= min_element(input.begin(), input.end());
int index = min_itr - input.begin();
We can also write same code using distance()
method.
vector<int>::iterator min_itr= min_element(input.begin(), input.end());
int index = distance(input.begin(), min_itr);
min_element
by default returns the first min element. To return the last min element in case of duplicates, we need to use rbegin()
and rend()
. Following are details on these
begin()
: Represents the starting pointer for the vector.end()
: Represents the imaginary element after the last element of the vector.rbegin()
: Represent the last element of the vector.rend()
: Represent the imaginary element just before the starting number of the vector.- When we use iterator using
begin()
andend()
, it iterates forward. - We use
rbegin()
andrend()
for backward iteration.
To find the last min element we need to apply reverse iteration as below.
int min (vector<int> input) {
auto min_itr= min_element(input.rbegin(), input.rend());
return *min_itr;
}
To return the index, use following.
int min (vector<int> input) {
auto min_itr= min_element(input.rbegin(), input.rend());
int index = input.rend() - min_itr -1;
return index;
}
We can also use comparator as below.
bool comparator (int a, int b) {
return a < b;
}
int min (vector<int> input) {
auto min_itr= min_element(input.rbegin(), input.rend(), comparator);
return *min_itr;
}
On set
We can also use min_element
on set
int min (set<int> input) {
auto min_itr= min_element(input.rbegin(), input.rend());
return *min_itr;
}
On unordered_set
Similarly we can apply on unordered_set
int min (unordered_set<int> input) {
auto min_itr= min_element(input.begin(), input.end(), comparator);
return *min_itr;
}
On map
We can also apply it on map
as below.
int min (map<int,int> input) {
auto min_itr= min_element(input.begin(), input.end());
return min_itr->first;
}
On unordered_map
Similarly, we can apply on unordered_map
as below.
int min (unordered_map<int,int> input) {
auto min_itr= min_element(input.begin(), input.end());
return min_itr->first;
}
On double linked list
We can also use min_element
on double linked sequence that supports both forward and backward traversal. Following is code for reference.
int min (list<int> input) {
auto min_itr= min_element(input.begin(), input.end());
return *min_itr;
}
On double ended queue
We can also use min_element
on Double-ended queues.
int min (deque<int> input) {
auto min_itr= min_element(input.begin(), input.end());
return *min_itr;
}
On custom iterator
We can use min_element
with custom iterator.
template <typename T>
class MyCustomContainer {
public:
void push_back(const T& element) {
// ... implementation
}
typename vector<T>::iterator begin() {
// ... implementation
}
typename vector<T>::iterator end() {
// ... implementation
}
private:
vector<T> data;
};
int main() {
MyCustomContainer<int> myContainer;
myContainer.push_back(5);
myContainer.push_back(2);
myContainer.push_back(8);
myContainer.push_back(1);
myContainer.push_back(9);
int minElement = *min_element(myContainer.begin(), myContainer.end());
minmax_element
The minmax_element finds the minimum and maximum elements within a range of values. It provides a convenient way to determine the extrema in a container or iterator range.
The function returns a pair
containing iterators to the minimum and maximum elements.
Following is code for reference.
int main() {
vector<int> numbers = {5, 2, 8, 1, 9};
// Find the minimum and maximum elements using minmax_element
auto result = minmax_element(numbers.begin(), numbers.end());
// Access the minimum and maximum elements
int min = *result.first;
int max = *result.second;
cout << "Minimum: " << min << endl;
cout << "Maximum: " << max << endl;
return 0;
}
Key Points:
minmax_element
is a generic algorithm that works with any container or iterator range that supports comparison.minmax_element
uses the<
operator for comparison, so ensure that your elements are comparable.- For custom comparison criteria, you can provide a custom comparison function as the third argument to
minmax_element
.
Happy learning c++ :-)