Given an array of integers, find the minimum value. Return the smallest element in the array. If the array is empty, return 0.
For example, the minimum of [3, 1, 4, 1, 5] is 1. The minimum of [10, 20, 30] is 10. The minimum of [-5, -2, -10, -1] is -10. A single-element array [5] has minimum 5. An empty array [] returns 0.
Finding the minimum (or maximum) value in a collection is one of the most basic and frequently used operations in programming. It appears in data analysis (lowest temperature reading, smallest transaction amount), graphics (bounding box computation), and algorithm design (priority queues, sorting).
The algorithm is straightforward: initialize a variable with the first element of the array, then iterate through the remaining elements. Whenever an element is smaller than the current minimum, update the minimum. After the loop, return the minimum. This approach examines each element exactly once and uses constant extra memory.
Edge cases include an empty array (return 0 to avoid accessing an invalid index), an array with all equal values (the minimum equals any element), and arrays containing negative numbers (the minimum will be negative and should be correctly identified).
Example Input & Output
First element is min
min of [3,1,4,1,5]
Single element
Negative numbers
Descending order
Algorithm Flow

Solution Approach
Iterate through the array, tracking the smallest value seen so far.
Start by checking for an empty array and returning 0 immediately. Initialize min to the first element. Then loop from index 1 to the end, comparing each element with min and updating min when a smaller value is found.
Many languages provide a built-in min function: Python's min(arr), JavaScript's Math.min(...arr), Java's Arrays.stream(nums).min().getAsInt(), or Rust's arr.iter().min(). These implement the same linear scan internally.
Time complexity is O(n), space complexity is O(1).
Best Answers
class Solution {
public int solution(int[] nums) {
if(nums.length==0)return 0;int m=nums[0];for(int n:nums){if(n<m)m=n;}return m;
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
