Python – Find the Maximum and Minimum Elements in an Array

Finding the maximum and minimum elements in an array is a fundamental problem in data structures and algorithms (DSA). In this tutorial, we will explore different ways to find the largest and smallest elements in an array using Python.

Problem Statement

Given an array arr of size n, find the maximum and minimum elements in the array.

Sample Input and Output

Example 1:

</>
Copy
Input: arr = [3, 1, 8, 2, 5, 10, 6]
Output: Maximum = 10, Minimum = 1

Example 2:

</>
Copy
Input: arr = [-5, -1, -9, 0, 3, 7]
Output: Maximum = 7, Minimum = -9

Solution Approach

There are multiple ways to find the maximum and minimum elements in an array. Below are three common approaches:

  1. Using Python’s Built-in Functions: The max() and min() functions return the largest and smallest elements.
  2. Using a Loop: Traverse the array to find the maximum and minimum manually.
  3. Using the Sorting Method: Sort the array and take the first and last elements.

Python Program

Method 1: Using Built-in max() and min() Functions

</>
Copy
# Function to find the maximum and minimum in an array using built-in functions
def find_max_min_builtin(arr):
    return max(arr), min(arr)

# Sample Input
arr1 = [3, 1, 8, 2, 5, 10, 6]
max_val, min_val = find_max_min_builtin(arr1)

# Printing the results
print("Maximum:", max_val)
print("Minimum:", min_val)

Output:

Maximum: 10
Minimum: 1

The max() and min() functions provide the simplest and most efficient way to find the largest and smallest elements.

Method 2: Using a Loop

</>
Copy
# Function to find max and min using a loop
def find_max_min_loop(arr):
    max_val = arr[0]
    min_val = arr[0]
    
    for num in arr[1:]:
        if num > max_val:
            max_val = num
        if num < min_val:
            min_val = num

    return max_val, min_val

# Sample Input
arr2 = [-5, -1, -9, 0, 3, 7]
max_val, min_val = find_max_min_loop(arr2)

# Printing the results
print("Maximum:", max_val)
print("Minimum:", min_val)

Output:

Maximum: 7
Minimum: -9

This method iterates through the array once, comparing each element to the current maximum and minimum values.

Method 3: Using Sorting

Sorting the array allows us to retrieve the minimum and maximum from the first and last elements.

</>
Copy
# Function to find max and min using sorting
def find_max_min_sort(arr):
    arr.sort()
    return arr[-1], arr[0]

# Sample Input
arr3 = [12, 4, 9, 1, 20, 7]
max_val, min_val = find_max_min_sort(arr3)

# Printing the results
print("Maximum:", max_val)
print("Minimum:", min_val)

Output:

Maximum: 20
Minimum: 1

The sorting method is not optimal in terms of efficiency as it requires O(n log n) time complexity, whereas the loop method runs in O(n) time.

Conclusion

We explored three different methods to find the maximum and minimum elements in an array:

  1. Built-in max() and min() functions: The simplest and most efficient approach.
  2. Using a loop: Efficient for large datasets with O(n) complexity.
  3. Using sorting: Works but is less efficient due to O(n log n) complexity.

For DSA interviews, the loop method is preferred as it runs in linear time with constant space.