0
0
PythonProgramBeginner · 2 min read

Python Program to Find Maximum in Array

To find the maximum in an array in Python, use the built-in function max(array) which returns the largest element in the list.
📋

Examples

Input[1, 2, 3, 4, 5]
Output5
Input[-10, 0, 10, 5]
Output10
Input[7]
Output7
🧠

How to Think About It

To find the maximum value, look at each number in the array one by one and keep track of the largest number found so far. At the end, the largest number you have is the maximum.
📐

Algorithm

1
Start with the first element as the maximum.
2
Go through each element in the array.
3
If the current element is greater than the maximum, update the maximum.
4
After checking all elements, return the maximum value.
💻

Code

python
def find_maximum(arr):
    max_value = arr[0]
    for num in arr:
        if num > max_value:
            max_value = num
    return max_value

# Example usage
array = [3, 5, 1, 2, 4]
print(find_maximum(array))
Output
5
🔍

Dry Run

Let's trace the array [3, 5, 1, 2, 4] through the code

1

Initialize max_value

max_value = 3 (first element)

2

Compare 3 with max_value

3 is not greater than 3, max_value stays 3

3

Compare 5 with max_value

5 is greater than 3, update max_value to 5

4

Compare 1 with max_value

1 is not greater than 5, max_value stays 5

5

Compare 2 with max_value

2 is not greater than 5, max_value stays 5

6

Compare 4 with max_value

4 is not greater than 5, max_value stays 5

7

Return max_value

Return 5 as the maximum

Current Numbermax_value
33
55
15
25
45
💡

Why This Works

Step 1: Start with first element

We assume the first element is the largest to have a starting point for comparison.

Step 2: Compare each element

We check each number with the current maximum using if num > max_value to find if there's a bigger number.

Step 3: Update maximum

If a bigger number is found, we update max_value to that number to keep track of the largest.

Step 4: Return result

After checking all numbers, the max_value holds the largest number in the array.

🔄

Alternative Approaches

Using built-in max() function
python
array = [3, 5, 1, 2, 4]
print(max(array))
This is the simplest and fastest way but hides the logic inside Python's built-in function.
Using sorting
python
array = [3, 5, 1, 2, 4]
array.sort()
print(array[-1])
Sorting the array and picking the last element works but is less efficient because sorting takes more time.

Complexity: O(n) time, O(1) space

Time Complexity

The program checks each element once, so it takes time proportional to the number of elements, which is O(n).

Space Complexity

The program uses only a few variables and does not create extra arrays, so space is O(1).

Which Approach is Fastest?

Using the built-in max() is fastest and simplest. Manual iteration is clear and efficient. Sorting is slower due to O(n log n) time.

ApproachTimeSpaceBest For
Manual iterationO(n)O(1)Learning and control over logic
Built-in max()O(n)O(1)Quick and clean code
SortingO(n log n)O(1)When array needs sorting anyway
💡
Use Python's built-in max() function for a quick and clean solution.
⚠️
Beginners often forget to initialize the maximum with the first element and start with zero or another number, which can cause wrong results.