Running Min
Start at arr[0]
Pick the first element, then beat it with smaller values.
Finding the minimum in an unsorted array is the twin of the max scan: keep a running winner and update it whenever you see a smaller value. This tutorial covers find_min, 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 smaller values.
Left → right
Visit each index once — no sorting required.
Reusable helper
Pass the array and size; return the smallest value.
Most negative
All-negative arrays: the min is the most negative.
Min = 2
Same seven numbers as Example 1, computed in the browser.
O(1) space
Same cost story as max; mention empty-array edges.
To find the minimum in a 1D array, pick the first element as your provisional winner. For each next element, if it is smaller than the current winner, replace the winner. After the last index, the winner is the minimum.
This is one pass, left to right — the same shape as finding the maximum, with < instead of >. No sorting required.
It locks in the running-state pattern after the max tutorial — comparisons, O(n) reasoning, and careful handling of negatives and empty arrays.
Never seed with a magic constant.
Update only when the next value is smaller.
Count elements without hard-coding.
One pass; constant extra space.
In short: set min = arr[0], then for each later element if arr[i] < min update min — and require size >= 1.
Given a non-empty array of integers, return the smallest value with a single left-to-right scan.
/* Array: 12, 5, 7, 3, 2, 8, 10
* Start min = 12
* 5 < 12 → min = 5
* 3 < 5 → min = 3
* 2 < 3 → min = 2
*/ | Item | Type | Description |
|---|---|---|
arr | const int[] | Input array; not modified by find_min. |
size | int | Element count; must be >= 1 for this tutorial. |
| return | int | Smallest value among the size elements. |
function find_min(arr, size): // assume size >= 1
min ← arr[0]
for i from 1 to size - 1:
if arr[i] < min:
min ← arr[i]
return min | Approach | Cost | Notes |
|---|---|---|
| Linear scan (this page) | O(n) | Best for min alone |
| Sort then take first | O(n log n) | Overkill for min only |
| Find maximum | O(n) | Same loops; use > instead |
| Goal | Pattern |
|---|---|
| Initialize | min_val = arr[0]; |
| Update | if (arr[i] < min_val) min_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 min and max share the same single-pass structure.
if < updateThis page — running min
if > updatePrevious page — flip the test
O(n log n)Unnecessary for min alone
arr[0] seedAvoid INT_MAX unless asked
Reach for a running minimum whenever you need the smallest value in an unsorted list.
Natural follow-up after finding the maximum.
Smallest score, temperature, or sensor value.
Basis for range (max − min) and clamp helpers.
Show why O(n) beats sorting for this goal.
Validate size before reading arr[0].
Key benefit: one clear scan that mirrors max — prove you can flip the comparison and still handle negatives correctly.
Uses the same seven integers as Example 1: 12, 5, 7, 3, 2, 8, 10.
Two complete C programs — a mixed sample (min 2) and an all-negative demo (min -9). Click View Output to reveal sample console results.
Initialize from the first element, then scan with <.
Matches the classic interview shape: find_min, a sample array, and length from sizeof. Uses int main(void). The minimum is 2.
#include <stdio.h>
int find_min(const int arr[], int size) {
int min_val = arr[0];
for (int i = 1; i < size; ++i) {
if (arr[i] < min_val) {
min_val = arr[i];
}
}
return min_val;
}
int main(void) {
int array[] = {12, 5, 7, 3, 2, 8, 10};
int size = (int)(sizeof(array) / sizeof(array[0]));
int min_value = find_min(array, size);
printf("Minimum value in the array: %d\n", min_value);
return 0;
} const int arr[] tells the reader (and the compiler) that this function will not change the array through arr. The cast on sizeof keeps size as int here; for huge arrays in real code, size_t is often nicer.
Same function still works when every value is negative.
Smallest still means “leftmost on the number line.” Here the minimum is −9, not −1.
#include <stdio.h>
int find_min(const int arr[], int size) {
int min_val = arr[0];
for (int i = 1; i < size; ++i) {
if (arr[i] < min_val) {
min_val = arr[i];
}
}
return min_val;
}
int main(void) {
int negatives[] = {-9, -3, -1, -7};
int n = (int)(sizeof(negatives) / sizeof(negatives[0]));
printf("Minimum (most negative): %d\n", find_min(negatives, n));
return 0;
} Starting at -9, later values are larger, so the min stays -9. Never initialize with 0 when negatives are possible — 0 would wrongly win as the “minimum.”
Assume size >= 1. If length can be zero, handle that before reading arr[0].
Set min = arr[0].
For i from 1 to size - 1, if arr[i] < min, set min = arr[i].
For the reference array, the answer is 2.
Trace the running minimum for {12, 5, 7, 3, 2, 8, 10}.
| i | arr[i] | Compare | min after |
|---|---|---|---|
| — | 12 | seed | 12 |
1 | 5 | 5 < 12? yes | 5 |
2 | 7 | 7 < 5? no | 5 |
3 | 3 | 3 < 5? yes | 3 |
4 | 2 | 2 < 3? yes | 2 |
5 | 8 | 8 < 2? no | 2 |
6 | 10 | 10 < 2? no | 2 |
Final answer: 2.
Where a running-minimum scan shows up beyond the interview prompt.
Master index loops and comparisons.
Example: twin of the max drill.
Lowest score, temperature, or reading.
Example: sensor batches.
Lead into O(n) vs sorting follow-ups.
Example: interview Q&A.
Prove most-negative wins among negatives.
Example: Example 2.
Same loops; flip the comparison.
Example: previous 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 min 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 min-finding code clean in interviews.
Avoid magic constants like 0 or INT_MAX 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 min alone.
Pro Tip: the walkthrough table is the fastest way to lock in updates before typing the loop.
Mistakes that commonly break minimum-finding solutions in C.
Fails when every element is negative (0 looks smaller than all of them? No — 0 is larger, so you miss the true min).
→ Always seed from arr[0] (or a true upper 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 min.
→ Prefer the linear scan.
Among {-9, -3, -1}, min is -9, max is -1.
→ Keep “leftmost on the number line” in mind.
Check these before calling the solution done.
size == 0There is no minimum; do not access arr[0] until you know the length is at least one.
size == 1The loop never runs; min_val stays that single element, which is correct.
Min is the most negative; seeding from arr[0] handles it.
Returning the value is fine; index-of-min needs an explicit tie policy.
Seeding from data still works when INT_MAX or INT_MIN appears.
Same structure with double; be careful with NaN in numerical code.
These programs use fixed numbers inside the code. To read values from the keyboard, use scanf in a loop, fill an array, then call find_min with the count you read.
| Sample | Result |
|---|---|
{12, 5, 7, 3, 2, 8, 10} | 2 |
{-9, -3, -1, -7} | -9 |
Try these variations to lock in the pattern.
{8, 1, 4, 1, 6}1size == 1< to >O(n) time, O(1) extra space.n = 0.Quick Takeaway: seed min 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 minimum is a one-pass running comparison: seed from the first element, update whenever a smaller 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 the multiplication table for a classic nested-loop warm-up.
min = arr[0], then update on arr[i] < min — 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 smaller
ScanOne loop
CodeMost negative
EdgeO(n)
AnalysisFinding the smallest value in an unsorted list still takes a single left-to-right pass: O(n) time and O(1) extra memory. You cannot do better in the worst case without extra structure, because any unseen element could be the new minimum.
Learn how to print a multiplication table with nested loops in C.
8 people found this page helpful