Find Maximum and Minimum in Array using Loop

Find Maximum and Minimum in Array using Loop

Visualization

Algorithm Steps

  1. Given an array of numbers arr.
  2. Initialize max_val and min_val with the first element of the array.
  3. Iterate through each element in the array starting from the second element.
  4. If the current element is greater than max_val, update max_val.
  5. If the current element is less than min_val, update min_val.
  6. After the loop, return max_val and min_val.

Find Maximum and Minimum using Loop Program Code

Python
Java
JavaScript
C
C++
C#
Kotlin
Swift
Go
Php
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)
print("Maximum:", max_val)
print("Minimum:", min_val)

Detailed Step by Step Example

Let us take the following array and apply the logic to find the maximum and minimum elements.

{ "array": [6,3,8,2,7,4], "showIndices": true, "specialIndices": [] }
{ "array": [0,6,6], "showIndices": false, "emptyIndices": [1, 2], "emptyCompIndices": [0], "labels": { "1": "max", "2": "min" } }

Initialize max = 6 and min = 6 with the first element of the array.

Check index 1

Compare 3 at index=1 with current max = 6 and min = 6.

3 is smaller than current min. Update min = 3.

{ "array": [6,3,8,2,7,4], "showIndices": true, "highlightIndices": [1], "specialIndices": [], "labels": {"1":"i"} }
{ "array": [0,6,3], "showIndices": false, "emptyIndices": [1, 2], "emptyCompIndices": [0], "labels": { "1": "max", "2": "min" } }

Check index 2

Compare 8 at index=2 with current max = 6 and min = 3.

8 is greater than current max. Update max = 8.

{ "array": [6,3,8,2,7,4], "showIndices": true, "highlightIndices": [2], "specialIndices": [], "labels": {"2":"i"} }
{ "array": [0,8,3], "showIndices": false, "emptyIndices": [1, 2], "emptyCompIndices": [0], "labels": { "1": "max", "2": "min" } }

Check index 3

Compare 2 at index=3 with current max = 8 and min = 3.

2 is smaller than current min. Update min = 2.

{ "array": [6,3,8,2,7,4], "showIndices": true, "highlightIndices": [3], "specialIndices": [], "labels": {"3":"i"} }
{ "array": [0,8,2], "showIndices": false, "emptyIndices": [1, 2], "emptyCompIndices": [0], "labels": { "1": "max", "2": "min" } }

Check index 4

Compare 7 at index=4 with current max = 8 and min = 2.

No update required.

{ "array": [6,3,8,2,7,4], "showIndices": true, "highlightIndices": [4], "specialIndices": [], "labels": {"4":"i"} }
{ "array": [0,8,2], "showIndices": false, "emptyIndices": [1, 2], "emptyCompIndices": [0], "labels": { "1": "max", "2": "min" } }

Check index 5

Compare 4 at index=5 with current max = 8 and min = 2.

No update required.

{ "array": [6,3,8,2,7,4], "showIndices": true, "highlightIndices": [5], "specialIndices": [], "labels": {"5":"i"} }
{ "array": [0,8,2], "showIndices": false, "emptyIndices": [1, 2], "emptyCompIndices": [0], "labels": { "1": "max", "2": "min" } }

Final Result:

Maximum = 8, Minimum = 2

{ "array": [6,3,8,2,7,4], "showIndices": true, "labels": { "2": "max", "3": "min" }, "specialIndices": [] }
{ "array": [0,8,2], "showIndices": false, "emptyIndices": [1, 2], "emptyCompIndices": [0], "labels": { "1": "max", "2": "min" } }