Start at the first element and update whenever a smaller value appears.
Calculation
min = 12
i=1 → 45 ≥ 12 → no change
i=2 → 2 < 12 → min = 2
i=3 → 42 ≥ 2 → no change
i=4 → 19 ≥ 2 → no change
Minimum = 2
Code
C++ Programs
Start with a simple running minimum. Then track the index when you also need the position.
Example 1 — Linear Scan
Keep a running minimum and update it whenever a smaller element appears.
C++
#include <iostream>
#include <stdexcept>
int findMin(const int arr[], int n) {
if (n <= 0) {
throw std::invalid_argument("Array must not be empty.");
}
int minVal = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] < minVal) {
minVal = arr[i];
}
}
return minVal;
}
int main() {
int numbers[] = { 12, 45, 2, 42, 19 };
int n = sizeof(numbers) / sizeof(numbers[0]);
std::cout << "Minimum = " << findMin(numbers, n) << "\n";
return 0;
}
Output
Minimum = 2
How It Works
Reject empty arrays (n <= 0) before reading arr[0].
Seed minVal with the first element.
Loop from index 1 through the rest of the array.
Update when arr[i] < minVal.
Return the final running minimum after one pass.
Example 2 — Track Index
Same scan, but also remember the first index where the minimum appears.
C++
#include <iostream>
#include <stdexcept>
int findMinIndex(const int arr[], int n) {
if (n <= 0) {
throw std::invalid_argument("Array must not be empty.");
}
int minIndex = 0;
for (int i = 1; i < n; i++) {
if (arr[i] < arr[minIndex]) {
minIndex = i;
}
}
return minIndex;
}
int main() {
int numbers[] = { 12, 45, 2, 42, 19 };
int n = sizeof(numbers) / sizeof(numbers[0]);
int index = findMinIndex(numbers, n);
std::cout << "Min = " << numbers[index]
<< " at index " << index << "\n";
return 0;
}
Output
Min = 2 at index 2
How It Works
Start with minIndex = 0 (first element is the candidate).
Compare each later element to arr[minIndex].
When a smaller value appears, store that new index.
Ties keep the earlier index because the test uses strict <.
Read the value with numbers[index] after the scan.
Edge Cases & Pitfalls
Check these before calling the solution done.
Empty
No minimum
Validate length before using arr[0].
Seed with 0
Wrong for positives
Seeding min = 0 fails when every element is positive.
Duplicates
Same answer
Multiple copies of the min still return that value.
Sorting
Overkill
One scan is enough; sorting is usually slower for this goal.
Analysis
Time and Space Complexity
Program
Time
Extra space
Linear scan
O(n)
O(1)
Track index
O(n)
O(1)
You need about n − 1 comparisons in the worst case. One pass is optimal for an unsorted array.
Remember
Key Takeaways
Pattern: seed with arr[0], then scan with <.
Empty: validate before reading the first element.
Index: same loop can track where the min sits.
Test: [12, 45, 2, 42, 19] → 2.
One line: one pass, keep the smaller value — that is the minimum.
Frequently Asked Questions
The first element is a valid candidate. Then every later element is compared against that running minimum.
An empty array has no minimum. Validate size (or n) > 0 and throw or return early.
Yes. The minimum is still the smallest value (for example 2 in [12, 45, 2, 19]).
No. Sorting is usually slower than one linear scan when you only need the minimum.
One pass over n elements: O(n) time and O(1) extra space.