Given an array of integers, the task is to find its minimum and maximum elements.
- The minimum element is the smallest value in the array.
- The maximum element is the largest value in the array.
Example:
Input: arr[] = {12, 1234, 45, 67, 1}
Output: Minimum = 1, Maximum = 1234Input: arr[] = {5, 89, 34, 12, 67}
Output: Minimum = 5, Maximum = 89
Approaches to Find Minimum and Maximum Element
The minimum and maximum elements can be found using the following approaches:
Iterative Approach
The simplest approach is to traverse the array once while keeping track of the smallest and largest elements found so far.
Steps
- Initialize both minimum and maximum with the first element.
- Traverse the remaining elements.
- Update the minimum if a smaller element is found.
- Update the maximum if a larger element is found.
#include <iostream>
#include <algorithm>
using namespace std;
int getMin(int arr[], int n)
{
int minVal = arr[0];
for (int i = 1; i < n; i++)
minVal = min(minVal, arr[i]);
return minVal;
}
int getMax(int arr[], int n)
{
int maxVal = arr[0];
for (int i = 1; i < n; i++)
maxVal = max(maxVal, arr[i]);
return maxVal;
}
int main()
{
int arr[] = {12, 1234, 45, 67, 1};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Minimum element: " << getMin(arr, n) << '\n';
cout << "Maximum element: " << getMax(arr, n);
return 0;
}
Output
Minimum element: 1 Maximum element: 1234
Explanation
- minVal and maxVal are initialized with the first element.
- The remaining elements are checked once to update the minimum and maximum values.
Recursive Approach
The problem can also be solved recursively by comparing the first element with the result obtained from the remaining array.
#include <iostream>
#include <algorithm>
using namespace std;
int getMin(int arr[], int n)
{
if (n == 1)
return arr[0];
return min(arr[0], getMin(arr + 1, n - 1));
}
int getMax(int arr[], int n)
{
if (n == 1)
return arr[0];
return max(arr[0], getMax(arr + 1, n - 1));
}
int main()
{
int arr[] = {12, 1234, 45, 67, 1};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Minimum element: " << getMin(arr, n) << '\n';
cout << "Maximum element: " << getMax(arr, n);
return 0;
}
Output
Minimum element: 1 Maximum element: 1234
Explanation
- Each recursive call compares the current first element with the result from the remaining array.
- Recursion stops when only one element remains.
Note: The recursive approach does not improve the time complexity over the iterative approach and uses additional stack space.
Using C++ Library Functions
C++ provides min_element() and max_element() to directly find the minimum and maximum elements in a range.
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int arr[] = {12, 1234, 45, 67, 1};
int n = sizeof(arr) / sizeof(arr[0]);
int minVal = *min_element(arr, arr + n);
int maxVal = *max_element(arr, arr + n);
cout << "Minimum element: " << minVal << '\n';
cout << "Maximum element: " << maxVal;
return 0;
}
Output
Minimum element: 1 Maximum element: 1234
Explanation
- min_element() returns an iterator pointing to the smallest element.
- max_element() returns an iterator pointing to the largest element.
- The * operator dereferences these iterators to obtain the values.