Easy 0 solved 0% acceptance

Array Minimum

Find the smallest value in a list.

What you'll learn

  • Find the smallest value in one pass
  • Compare each element with the current best
  • Avoid a wrong starting value

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

Example 1
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.

Example 2
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.

Learn from others

Community solutions

No published solutions yet. Solve this challenge to share the first one.