Running Max
Start at arr[0]
Pick the first element, then beat it with larger values.
Finding the maximum in an unsorted array is a classic linear scan: keep a running winner and update it whenever you see a larger value. This tutorial covers find_max, sizeof for length, a live preview, worked C examples (mixed positives and all negatives), edge cases, and O(n) complexity.
Start at arr[0]
Pick the first element, then beat it with larger values.
Left → right
Visit each index once — no sorting required.
Reusable helper
Pass the array and size; return the largest value.
Still works
All-negative arrays: the max is the least negative.
Max = 42
Same six numbers as Example 1, computed in the browser.
O(1) space
Optimal for an unsorted scan; mention empty-array edges.
To find the maximum in a 1D array, pick the first element as your provisional winner. For each next element, if it is greater than the current winner, replace the winner. After the last index, the winner is the maximum.
This is one pass, left to right — easy to code and easy to explain in an interview. No sorting required.
It is the foundational array scan: running state, comparisons, and O(n) reasoning — skills reused for min, average, and search problems.
Never seed with a magic constant.
Update only when the next value is larger.
Count elements without hard-coding.
One pass; constant extra space.
In short: set max = arr[0], then for each later element if arr[i] > max update max — and require size >= 1.
Given a non-empty array of integers, return the largest value with a single left-to-right scan.
/* Array: 14, 7, 25, 31, 10, 42
* Start max = 14
* 25 > 14 → max = 25
* 31 > 25 → max = 31
* 42 > 31 → max = 42
*/ | Item | Type | Description |
|---|---|---|
arr | const int[] | Input array; not modified by find_max. |
size | int | Element count; must be >= 1 for this tutorial. |
| return | int | Largest value among the size elements. |
function find_max(arr, size): // assume size >= 1
max ← arr[0]
for i from 1 to size - 1:
if arr[i] > max:
max ← arr[i]
return max | Approach | Cost | Notes |
|---|---|---|
| Linear scan (this page) | O(n) | Best for max alone |
| Sort then take last | O(n log n) | Overkill for max only |
| Find minimum | O(n) | Same loops; use < instead |
| Goal | Pattern |
|---|---|
| Initialize | max_val = arr[0]; |
| Update | if (arr[i] > max_val) max_val = arr[i]; |
| Length | sizeof(array) / sizeof(array[0]) |
| Guard | Require size >= 1 before reading arr[0] |
| Cost | O(n) time, O(1) extra space |
Related array problems — only max and min share the same single-pass structure.
if > updateThis page — running max
if < updateNext page — flip the test
O(n log n)Unnecessary for max alone
arr[0] seedAvoid INT_MIN unless asked
Reach for a running maximum whenever you need the largest value in an unsorted list.
First array question for many beginners.
Highest reading in a batch of samples.
Basis for min, range, and clamp helpers.
Show why O(n) beats sorting for this goal.
Validate size before reading arr[0].
Key benefit: one clear scan that proves you understand running state, comparisons, and complexity without overengineering.
Uses the same six integers as Example 1: 14, 7, 25, 31, 10, 42.
Two complete C programs — a mixed positive sample (max 42) and an all-negative demo (max -1). Click View Output to reveal sample console results.
Initialize from the first element, then scan with >.
Matches the classic walkthrough: find_max, sample array, and sizeof length. Uses int main(void).
#include <stdio.h>
int find_max(const int arr[], int size) {
int max_val = arr[0];
for (int i = 1; i < size; ++i) {
if (arr[i] > max_val) {
max_val = arr[i];
}
}
return max_val;
}
int main(void) {
int array[] = {14, 7, 25, 31, 10, 42};
int size = (int)(sizeof(array) / sizeof(array[0]));
int max_value = find_max(array, size);
printf("Maximum value in the array: %d\n", max_value);
return 0;
} const int arr[] promises not to modify elements through arr. The cast on sizeof keeps size as int for this tutorial; with very large arrays prefer size_t.
Same function still works when every value is negative.
The “maximum” is the least negative value (here −1). Seeding from arr[0] is why this works without special cases.
#include <stdio.h>
int find_max(const int arr[], int size) {
int max_val = arr[0];
for (int i = 1; i < size; ++i) {
if (arr[i] > max_val) {
max_val = arr[i];
}
}
return max_val;
}
int main(void) {
int negatives[] = {-9, -3, -1, -7};
int n = (int)(sizeof(negatives) / sizeof(negatives[0]));
printf("Maximum (least negative): %d\n", find_max(negatives, n));
return 0;
} Starting at -9, the scan promotes to -3, then -1. Never initialize with 0 when negatives are possible — 0 would wrongly win.
Require size >= 1 (or handle empty arrays explicitly in production code).
Set max = arr[0].
For i from 1 to size - 1, if arr[i] > max, set max = arr[i].
For the reference array, the answer is 42.
Trace the running maximum for {14, 7, 25, 31, 10, 42}.
| i | arr[i] | Compare | max after |
|---|---|---|---|
| — | 14 | seed | 14 |
1 | 7 | 7 > 14? no | 14 |
2 | 25 | 25 > 14? yes | 25 |
3 | 31 | 31 > 25? yes | 31 |
4 | 10 | 10 > 31? no | 31 |
5 | 42 | 42 > 31? yes | 42 |
Final answer: 42.
Where a running-maximum scan shows up beyond the interview prompt.
Master index loops and comparisons.
Example: first array drill.
Highest score, temperature, or reading.
Example: sensor batches.
Lead into O(n) vs sorting follow-ups.
Example: interview Q&A.
Prove negatives do not break the scan.
Example: Example 2.
Same loops; flip the comparison next.
Example: next tutorial.
Discuss empty and single-element arrays.
Example: production guards.
Pro Tip: say “seed from arr[0], then update on >” before writing the loop — and mention empty arrays if asked.
Why the linear scan earns interview points.
You must look at every element at least once; one pass is enough.
Only a few scalars besides the input array.
Seeding from arr[0] handles all-negative data.
Whiteboard the running max cell by cell.
Pro Tip: do not sort first unless you need the full order — mention that if the interviewer probes alternatives.
Small habits that keep max-finding code clean in interviews.
Avoid magic constants like 0 or INT_MIN unless required.
You already accounted for index 0 as the seed.
State that size >= 1 or return an error path.
It works on real arrays, not decayed pointers in callees.
Mention you do not need to sort for max alone.
Pro Tip: the walkthrough table is the fastest way to lock in updates before typing the loop.
Mistakes that commonly break maximum-finding solutions in C.
Fails when every element is negative.
→ Always seed from arr[0] (or a true lower bound).
Reading arr[0] when size == 0 is undefined.
→ Validate length before the scan.
Inside a function that received a decayed pointer, sizeof is wrong.
→ Pass size explicitly from the caller.
Wastes work when you only need the max.
→ Prefer the linear scan.
If you also need the index of the first max, prefer > so ties keep the earlier index.
→ Decide tie-breaking rules explicitly.
Check these before calling the solution done.
size == 0No maximum exists; guard before reading arr[0].
size == 1The loop body never runs; max_val stays correct as that single element.
Max is the least negative; seeding from arr[0] handles it.
Returning the value is fine; index-of-max needs an explicit tie policy.
Seeding from data still works when INT_MIN appears in the array.
Same structure with double; be careful with NaN in numerical code.
Programs embed literals. To read from the user, call scanf in a loop after reading n, then pass the filled array and n to find_max.
| Sample | Result |
|---|---|
{14, 7, 25, 31, 10, 42} | 42 |
{-9, -3, -1, -7} | -1 |
Try these variations to lock in the pattern.
{3, 9, 2, 9, 5}9size == 1> to <O(n) time, O(1) extra space.n = 0.Quick Takeaway: seed max from arr[0], update on >, require a non-empty array, and quote O(n).
| Approach | Time | Extra space |
|---|---|---|
| Single scan | O(n) | O(1) |
Finding the maximum is a one-pass running comparison: seed from the first element, update whenever a larger value appears, and return the winner. Master both the mixed and all-negative samples so you can explain negatives and empty-array edges confidently.
Practice both examples above, then continue to finding the minimum — same loops with <.
max = arr[0], then update on arr[i] > max — with size >= 1.
arr[0]size >= 1 (or handle empty)O(n) time and O(1) space0 blindlyarr[0] when the array is emptysizeof on a decayed pointerImplement it the interview-friendly way.
arr[0]
InitOn larger
ScanOne loop
CodeNegatives OK
EdgeO(n)
AnalysisFinding the maximum in an unsorted array needs at least n − 1 comparisons in the worst case (standard adversary argument). A single left-to-right scan achieves O(n) time and O(1) extra space.
Learn how to find the smallest element with the same linear-scan pattern in C.
8 people found this page helpful