Find Maximum Value of an Array in Python
What you’ll learn
- The classic linear scan with a running maximum.
- How to write a reusable
find_maxhelper in Python. - A second all-negative example and a live preview.
Overview
One left-to-right pass is enough to find the largest number in an unsorted list.
Two examples
Mixed positive list and all-negative list.
Live preview
Shows max for the same sample data as code example 1.
Interview focus
Explain O(n) scan and empty-list handling.
Prerequisites
Basic loops and lists in Python.
- Understand list indexing and
forloops. - Know that Python lists can contain negative and positive integers.
The idea
Start with the first element as current maximum. Whenever you find a larger number, update the maximum.
After one pass, the current maximum is the answer.
Live preview
Uses the same sample list as example 1: 14, 7, 25, 31, 10, 42.
Algorithm
Goal: return the largest value in a list of n integers.
Handle empty list
If list is empty, raise an error or return a special value.
Initialize maximum
Set max_val = arr[0].
Scan remaining values
For each next element, update max_val if current element is larger.
Return answer
Return final max_val.
📜 Pseudocode
function find_max(arr):
if arr is empty:
error
max_val <- arr[0]
for each value in arr from second element:
if value > max_val:
max_val <- value
return max_val Find maximum (reference program)
A reusable helper function and a sample list with answer 42.
def find_max(arr: list[int]) -> int:
if not arr:
raise ValueError("Array must contain at least one element.")
max_val = arr[0]
for i in range(1, len(arr)):
if arr[i] > max_val:
max_val = arr[i]
return max_val
def main() -> None:
array = [14, 7, 25, 31, 10, 42]
max_value = find_max(array)
print(f"Maximum value in the array: {max_value}")
if __name__ == "__main__":
main() Explanation
The function raises a clear error for empty input and uses one simple pass for normal lists.
When every element is negative
The same algorithm still works; maximum is the least negative value.
def find_max(arr: list[int]) -> int:
if not arr:
raise ValueError("Array must contain at least one element.")
max_val = arr[0]
for x in arr[1:]:
if x > max_val:
max_val = x
return max_val
def main() -> None:
negatives = [-9, -3, -1, -7]
print(f"Maximum (least negative): {find_max(negatives)}")
if __name__ == "__main__":
main() Notes
Built-in option. Python has max(arr), but interviewers often ask you to write the manual scan first.
Huge data. Parallel scans can combine partial maxima in large-scale systems.
❓ FAQ
🔄 Input / output
You can read integers from input, store them in a list, and pass that list to find_max.
Edge cases
Empty list
No maximum exists; handle this before scanning.
One element
That element is automatically the maximum.
⏱️ Time and space complexity
| Approach | Time | Extra space |
|---|---|---|
| Single scan | O(n) | O(1) |
Summary
- Algorithm: running maximum in one pass.
- Cost:
O(n)time,O(1)extra space. - Important edge: define behavior for empty list.
Finding the maximum in an unsorted list needs at least n - 1 comparisons in the worst case. A single left-to-right scan gives O(n) time and O(1) extra space.
8 people found this page helpful
