Introduction
Finding the maximum and minimum element in an array means identifying the largest and smallest values present in the array.
Given an array of integers, arr[] of size n, the task is to find both the maximum and minimum elements in the array.
This is one of the most fundamental array problems and helps in understanding array traversal, comparison operations, and efficient searching techniques.
Example:
Input: arr[] = [4, 2, 9, 1, 7]Output: Maximum = 9 Minimum = 1 Explanation: Largest value present in the array is 9 Smallest value present in the array is 1
Input: arr[] = [12, 15, 3, 20, 8] Output: Maximum = 20 Minimum = 3 Explanation: Largest value present in the array is 20 Smallest value present in the array is 3
Constraints:
1 <= n <= 10^5-10^9 <= arr[i] <= 10^9Approach 1 : Brute Force
Explanation
The simplest way to solve this problem is to compare every element with the current maximum and minimum values.
During traversal:
- If an element is greater than the current maximum, update maximum.
- If an element is smaller than the current minimum, update minimum.
This approach is simple and easy to understand.
Steps
- Initialize:
- maximum = first element
- minimum = first element
- Traverse the array from index 1.
- Compare each element:
- update maximum if current element is greater
- update minimum if current element is smaller
- Print the final maximum and minimum values.
Input Array: [4, 2, 9, 1, 7] Initially: Maximum = 4Minimum = 4 Traverse 2: 2 is smaller than current minimum (4) Update Minimum = 2 Traverse 9: 9 is greater than current maximum (4) Update Maximum = 9 Traverse 1: 1 is smaller than current minimum (2) Update Minimum = 1 Traverse 7: 7 is neither greater than maximum nor smaller than minimum No update required Final Result: Maximum = 9 Minimum = 1
Brute Force Code
Complexity Analysis
Time Complexity: O(n)Explanation: the array is traversed only once. Each element is compared with maximum and minimum values.
Space Complexity: O(1) Explanation: No extra data structures are used. Only a few variables are required.
Approach 2 : Optimized Solution
Explanation
The brute force solution itself is already an optimized solution because every element must be checked at least once to determine the maximum and minimum values.
Therefore:
- O(n) is the best possible time complexity.
- Constant extra space is sufficient.
This makes the traversal approach both simple and optimal.
Steps
- Initialize:
- maximum = first element
- minimum = first element
- Traverse the array once.
- Compare each element:
- update maximum if needed
- update minimum if needed
- Return final maximum and minimum values.
Input Array: [12, 15, 3, 20, 8]Initially: Maximum = 12
Minimum = 12 Traverse 15: 15 is greater than current maximum Update Maximum = 15 Traverse 3: 3 is smaller than current minimum Update Minimum = 3 Traverse 20: 20 is greater than current maximum Update Maximum = 20 Traverse 8: 8 is neither greater than maximum nor smaller than minimum No update required Final Result: Maximum = 20 Minimum = 3
Optimized Code