Find Minimum Value of an Array in C++
What you’ll learn
- What “minimum” means for a list of numbers, without assuming you already code every day.
- The usual one-pass trick: remember the best answer so far and update it when you see something smaller.
- A small reusable function
find_min, counting elements withsizeof, andint main(). - A second example where every value is negative, plus a live preview that uses the same numbers as example 1.
Overview
Picture a row of numbers on the board. To find the smallest, you can walk from left to right and keep the best (“smallest so far”) in your head. A computer does the same with a variable—no sorting, no magic.
Two programs
A mixed list (answer 2) and a list of only negatives (answer −9).
Live preview
Try the button: it uses the same eight integers as example 1.
Interview hook
Say O(n) time, O(1) extra space, and mention empty arrays if the interviewer asks.
Prerequisites
for loops, arrays, and std::cout.
#include <iostream>and a simple array likeint a[] = { ... }.- Optional but handy:
sizeof(array) / sizeof(array[0])tells you how many slots the array has when you did not hard-code the length.
The idea
Start by treating the first number as the smallest you have seen. For each next number, ask: “Is this smaller than what I am holding?” If yes, replace your answer. After you visit every position, what you hold is the minimum for the whole array.
One loop, one variable—easy to follow on paper and in an interview.
Live preview
Same eight integers as example 1: 12, 5, 7, 3, 2, 8, 10.
Algorithm
Goal: return the smallest value among n integers stored one after another in memory (a C array).
Check length
For this tutorial, assume size >= 1. If the length can be zero, handle that before you read arr[0].
Initialize
Set min = arr[0].
Scan
For i from 1 to size - 1, if arr[i] < min, set min = arr[i].
Return
Return min.
📜 Pseudocode
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 Find minimum (reference program)
Matches the classic interview shape: a helper find_min, a sample array, and length from sizeof. Uses int main(). The sample matches the old walkthrough: minimum is 2.
#include <iostream>
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() {
int array[] = {12, 5, 7, 3, 2, 8, 10};
int size = (int)(sizeof(array) / sizeof(array[0]));
int min_value = find_min(array, size);
std::cout << "Minimum value in the array: " << min_value << "\n";
return 0;
} Explanation
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.
When every element is negative
Smallest still means “leftmost on the number line.” Here the minimum is −9, not −1.
#include <iostream>
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() {
int negatives[] = {-9, -3, -1, -7};
int n = (int)(sizeof(negatives) / sizeof(negatives[0]));
std::cout << "Minimum (most negative): " << find_min(negatives, n) << "\n";
return 0;
} Notes
Parallel systems. You can split the array, find each part’s minimum, then take the minimum of those—same idea as parallel max, but with smaller-is-better.
Floating-point. The same loop works with double; in numerical code be careful with NaN and with comparing floats for exact equality.
❓ FAQ
🔄 Input / output
These programs use fixed numbers inside the code. To read values from the keyboard, you would use std::cin in a loop, fill an array, then call find_min with the count you read.
Edge cases
size == 0
There is no minimum; do not access arr[0] until you know the length is at least one.
size == 1
The loop never runs; min_val stays that single element, which is correct.
⏱️ Time and space complexity
| Approach | Time | Extra space |
|---|---|---|
| Single scan | O(n) | O(1) |
Summary
- Algorithm: keep a running minimum while scanning once.
- Cost:
O(n)time,O(1)extra space. - Edge: decide what to do when
n = 0.
Finding 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.
8 people found this page helpful
