Find Maximum Value of an Array in C
What you’ll learn
- The classic linear scan: keep a running maximum and update it whenever you see a larger element.
- A reusable
find_maxfunction,sizeofto count elements, andint main(void). - A second example with all negative values, plus a live preview for the reference array.
Overview
You walk the array once. The answer after visiting every slot is the largest value stored—no sorting required.
Two programs
Reference mix of positives and an all-negative demo.
Live preview
Same six numbers as example 1; prints the max in the page.
Interview hook
Mention O(n) time and the empty-array edge case when asked.
Prerequisites
for loops, arrays, and printf.
#include <stdio.h>and basic array declarationint a[] = { ... }.- Understanding
sizeof(array) / sizeof(array[0])to get length when the size is not written by hand.
The idea
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.
Live preview
Uses the same six integers as example 1: 14, 7, 25, 31, 10, 42.
Algorithm
Goal: return the largest value among n integers in contiguous memory.
Validate length
Require size >= 1 (or handle empty arrays explicitly in production code).
Initialize
Set max = arr[0].
Scan
For i from 1 to size - 1, if arr[i] > max, set max = arr[i].
Return
Return max.
📜 Pseudocode
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 Find maximum (reference program)
Matches the reference behavior: 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;
} Explanation
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.
When every element is negative
The same function still works: the “maximum” is the least negative value (here −1).
#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;
} Notes
Parallel systems. You can split the array and take the max of partial maxima, then combine—useful for very large data in other environments.
Floating-point. Same structure with double; avoid strict equality when comparing floats for “maximum” in numerical code.
❓ FAQ
🔄 Input / output
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.
Edge cases
size == 0
No maximum exists; guard before reading arr[0].
size == 1
The loop body never runs; max_val stays correct as that single element.
⏱️ Time and space complexity
| Approach | Time | Extra space |
|---|---|---|
| Single scan | O(n) | O(1) |
Summary
- Algorithm: running maximum from left to right.
- Cost:
O(n)time,O(1)extra space. - Edge: define behavior when
n = 0.
Finding 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.
8 people found this page helpful
