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^9

Approach 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:

  1. If an element is greater than the current maximum, update maximum.
  2. If an element is smaller than the current minimum, update minimum.

This approach is simple and easy to understand.

Steps

  1. Initialize:
    • maximum = first element
    • minimum = first element
  2. Traverse the array from index 1.
  3. Compare each element:
    • update maximum if current element is greater
    • update minimum if current element is smaller
  4. Print the final maximum and minimum values. 
Dry Run:
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

  1. Initialize:
    • maximum = first element
    • minimum = first element
  2. Traverse the array once.
  3. Compare each element:
    • update maximum if needed
    • update minimum if needed
  4. 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

Complexity Analysis

Time Complexity: O(n)Explanation:
Every element is visited exactly once.

Space Complexity: O(1) Explanation: Only two extra variables are used: maximum and minimum.

Edge Cases

  1. Array contains only one element
  2. Array contains all equal elements
  3. Array contains negative numbers
  4. Maximum and minimum values are repeated

Why This Problem is Important

This problem helps in understanding:

  1. Array traversal
  2. Comparison operations
  3. Efficient searching techniques
  4. Optimization basics
  5. Conditional updates

It is one of the most important beginner-level array problems.

Real-World Applications

Finding maximum and minimum values is used in:

  1. Stock market analysis
  2. Sensor data processing
  3. Performance monitoring
  4. Statistical analysis
  5. Data analytics systems

Common Mistakes

  1. Incorrect initialization
  2. Forgetting negative number cases
  3. Comparing wrong variables
  4. Traversing from incorrect index

Interview Tips

Interviewers often expect:

  1. An O(n) traversal solution
  2. Constant space optimization
  3. Proper handling of edge cases

Always explain why checking every element once is sufficient.

Related Questions

  1. Second Largest Element
  2. Leaders in an Array
  3. Maximum Subarray Sum
  4. Kth Largest Element
  5. Find Peak Element

Final Takeaway

Finding the maximum and minimum element in an array is one of the most fundamental DSA problems. It teaches efficient traversal, comparison operations, and optimization concepts that are widely used in coding interviews and real-world applications.