Maximum and Minimum Element in an Array
An easy Arrays problem included in Apna College, Love Babbar 450. Below: the roles whose interviews prioritise this topic, and how to practise it.
- Topic
- Arrays
- Sheets
- 2
- Core for
- 25 roles
- Platform
- GeeksforGeeks
The problem
Given an array of integers, find both the maximum and minimum elements present in the array.
Example 1
- Input
- [3,1,4,1,5,9,2,6]
- Output
- max=9, min=1
Example 2
- Input
- [42]
- Output
- max=42, min=42
Constraints
- 1 <= n <= 10^5
- -10^9 <= arr[i] <= 10^9
How to think about it
Updated 2026-09-09The question only ever needs two numbers held in your hand, so the array only needs to be read once. Reaching for a sort is the common reflex and it is strictly worse: it buys full ordering, an answer to a much harder question, and charges O(n log n) for it.
Approaches, worst first
Sort and take the ends
time O(n log n) · space O(1)
Sort ascending, then the first and last elements are the answer. Correct, one line, and it does far more work than the question asks for — you have ordered every element against every other to learn two of them.
Two separate passes
time O(n) · space O(1)
Scan once for the maximum, once for the minimum. Already optimal in big-O and the version most people write; it just touches every element twice.
One pass, two accumulatorsWrite this one
time O(n) · space O(1)
Seed both from arr[0], then for each later element update whichever bound it beats. A tighter variant compares elements in pairs first and feeds the smaller to min and the larger to max, which lands at 3n/2 comparisons instead of 2n.
Where people lose marks · 4
- Seeding max with 0 instead of arr[0]. On an all-negative array it reports 0 as the maximum, and the bug survives every test case that happens to contain a positive.
- Seeding min with a language MAX_INT constant is safe until the array legitimately contains that value — seeding from arr[0] never has that failure mode.
- A one-element array must return that element as both max and min, not an error.
- `else if` when updating both bounds skips the min check whenever the max updated. It only looks correct because arr[0] seeded both.
Full solution
One pass with two accumulators seeded from arr[0]: the question only ever needs two numbers, so a single linear scan is optimal and sidesteps the sort reflex that pays O(n log n) for a full ordering nobody asked for.
Python
def find_max_min(arr: list[int]) -> tuple[int, int]:
# Seed both bounds from arr[0]: 0 or MAX_INT seeds break on all-negative / edge inputs.
largest = smallest = arr[0]
for x in arr[1:]:
if x > largest:
largest = x
if x < smallest: # independent `if`, not `elif`: both bounds must be checked
smallest = x
return largest, smallest
JavaScript
function findMaxMin(arr) {
// Seed both bounds from arr[0]: 0 or MAX_INT seeds break on all-negative / edge inputs.
let largest = arr[0];
let smallest = arr[0];
for (let i = 1; i < arr.length; i++) {
if (arr[i] > largest) largest = arr[i];
if (arr[i] < smallest) smallest = arr[i]; // independent `if`, not `else if`
}
return [largest, smallest];
}
The theory behind it
Arrays — the ground this problem stands on. All Arrays problems
What Arrays is
An array is a row of fixed boxes laid side by side in computer memory, like numbered lockers in a hallway. Because each box occupies identical space and sits directly next to its neighbors, jumping to locker zero or locker ten thousand takes the exact same tiny fraction of time. Every box holds an item of the same type, addressed by an offset number called an index.
When to reach for it
Reach for an array when items arrive in a known sequence and need immediate retrieval by position number. Problems asking for running totals, prefix accumulations, cyclic rotations, or in-place rearrangements signal array mechanics. Whenever constraints require constant-time random lookups or contiguous cache scans across fixed collections, a flat sequence is the default container.
How the pattern works
Visualize a tape with zero-indexed slots stretching from start to end. Keep track of write and read cursors when modifying contents without allocating helper buffers. For running computations, maintain an invariant such as having processed all elements left of the current index while pending elements wait to the right. When modifying entries in place, consider scanning backwards from the end so unread data is not overwritten.
What each operation costs
| Operation | Time |
|---|---|
| look up element by index | O(1) |
| insert or delete at the start | O(n) |
| search an unsorted collection for a value | O(n) |
What usually goes wrong with Arrays
- Reading past the final index by checking index less than or equal to length instead of strictly less than length, triggering index out of bounds exceptions.
- Modifying length or removing elements during a forward iteration loop, which causes remaining items to shift left and skip validation on the next neighbor.
- Assuming dynamic resizing is costless inside nested loops, causing repeated memory reallocation copies when appending unknown quantities of items.
Which roles need this problem
Arrays is a core topic for these 25 roles — if you're targeting one of them, this problem is early in your path, not optional.
Secondary for 3 more roles, including Database Engineer, Bioinformatics Engineer, Networking Engineer.
Track this in your role's order
Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.
Start freeMore Arrays problems
Problem set and role mapping as of .