Problem
Return the smallest value in the list by checking each element once.
Implement array_minimum(numbers).
Input
numbers: A non-empty list of integers.
Output
Return the minimum.
Constraints
1 ≤ length ≤ 100,000
Examples
Input:
numbers = [3, 9, 2]
Expected output:
expected = 2
Input:
std::vector<long long> numbers = {3, 9, 2};
Expected output:
long long expected = 2;
Input:
long[] numbers = new long[] {3, 9, 2};
Expected output:
long expected = 2;
Explanation
2 is smaller than 3 and 9.
Input:
numbers = [0, -5, 5]
Expected output:
expected = -5
Input:
std::vector<long long> numbers = {0, -5, 5};
Expected output:
long long expected = -5;
Input:
long[] numbers = new long[] {0, -5, 5};
Expected output:
long expected = -5;
Explanation
-5 is the smallest.
How to approach it
Try on your own first, then open one hint at a time.
Hint 1
Track the smallest value seen while scanning the list once.
Hint 2
Compare each element to your current best and update only when smaller.
Hint 3
Remember the list is non-empty, so you can start from the first element.
Expected complexity
O(n) time, O(1) extra space
References
This challenge is adapted for CodeUtility. Follow the statement and constraints above.