The maximum is the largest element in the list. For an unsorted list, scan once from left to right and keep a running best.
Remember
arr = [12, 45, 7, 42, 19]
max = 45
Idea:
max = arr[0]
for each later element x:
if x > max → max = x
Works for negatives too:
[-8, -1, -15] → max = -1
Array
Maximum
[12, 45, 7, 42, 19]
45
[-8, -1, -15]
-1
[5]
5
[]
undefined — handle empty
Approach
How to Find the Maximum in Python
Four clear steps — one linear pass is enough.
Reject an empty list (no maximum exists).
Set max_val = arr[0] as the first candidate.
Loop from index 1 to the end.
Whenever arr[i] > max_val, update max_val (optionally track the index too).
Method
Idea
Time
Best for
Linear scan
Running maximum with >
O(n)
Interviews, unsorted lists
Track index
Same scan, also store the position
O(n)
When you need where the max is
Pseudocode
Pseudocode
function FindMax(arr):
if length(arr) == 0:
error "empty array"
max = arr[0]
for i from 1 to length(arr) - 1:
if arr[i] > max:
max = arr[i]
return max
Reference
Cheat Sheet
Keep these facts handy while writing the program.
Topic
Detail
Start
max_val = arr[0]
Update
if arr[i] > max_val
Empty
Validate before reading arr[0]
Negatives
Still use > — do not assume positive
Avoid
Sorting just to get the max
Classic check
[12, 45, 7, 42, 19] → 45
Try it
Live Preview
Pick a sample array to see the running maximum after each step.
Tap a chip to run the linear scan trace.
Result
arr = [12, 45, 7, 42, 19]
start max = 12
45 > 12 → max = 45
7 ≤ 45
42 ≤ 45
19 ≤ 45
Maximum = 45
Trace
Worked Walkthrough — [12, 45, 7, 42, 19]
Start at the first element and update whenever a larger value appears.
Calculation
max = 12
i=1 → 45 > 12 → max = 45
i=2 → 7 ≤ 45 → no change
i=3 → 42 ≤ 45 → no change
i=4 → 19 ≤ 45 → no change
Maximum = 45
Code
Python Programs
Start with a simple running maximum. Then track the index when you also need the position.
Example 1 — Linear Scan
Keep a running maximum and update it whenever a larger element appears.
python
def find_max(arr):
if not arr:
raise ValueError("Array must not be empty.")
max_val = arr[0]
for i in range(1, len(arr)):
if arr[i] > max_val:
max_val = arr[i]
return max_val
numbers = [12, 45, 7, 42, 19]
print("Maximum =", find_max(numbers))
Output
Maximum = 45
How It Works
Reject an empty list before reading arr[0].
Seed max_val with the first element.
Loop from index 1 so you never compare the seed against itself needlessly.
Update when arr[i] > max_val.
Return the final running maximum after one pass.
Example 2 — Track Index
Same scan, but also remember the first index where the maximum appears.
python
def find_max_index(arr):
if not arr:
raise ValueError("Array must not be empty.")
max_index = 0
for i in range(1, len(arr)):
if arr[i] > arr[max_index]:
max_index = i
return max_index
numbers = [12, 45, 7, 42, 19]
index = find_max_index(numbers)
print(f"Max = {numbers[index]} at index {index}")
Output
Max = 45 at index 1
How It Works
Start with max_index = 0 (first element is the candidate).
Compare each later element to arr[max_index].
When a larger value appears, store that new index.
Ties keep the earlier index because the test uses strict >.
Read the value with numbers[index] after the scan.
Edge Cases & Pitfalls
Check these before calling the solution done.
Empty
No maximum
Validate length before using arr[0].
Negatives
Still works
Do not seed with 0 — that fails for all-negative lists.
Duplicates
Same answer
Multiple copies of the max still return that value.
Built-in
max() later
Interviews often want the loop first; max(arr) is fine afterward.
Analysis
Time and Space Complexity
Program
Time
Extra space
Linear scan
O(n)
O(1)
Track index
O(n)
O(1)
You need about n − 1 comparisons in the worst case. One pass is optimal for an unsorted list.
Remember
Key Takeaways
Pattern: seed with arr[0], then scan with >.
Empty: validate before reading the first element.
Index: same loop can track where the max sits.
Test: [12, 45, 7, 42, 19] → 45.
One line: one pass, keep the larger value — that is the maximum.
Frequently Asked Questions
The first element is a valid candidate. Then every later element is compared against that running maximum.
An empty list has no maximum. Validate len(arr) > 0 and raise or return early.
Yes. The maximum is still the greatest value, even if it is negative (for example −1 is greater than −8).
No. Sorting is usually slower than one linear scan when you only need the maximum.
One pass over n elements: O(n) time and O(1) extra space.