This problem asks for the smallest value in the array. You are not looking for a subarray or a special pattern. You just need the minimum element among all the numbers provided.
The answer is the value itself, not the position where it appears. Negative numbers matter here too. If an array includes negative values, the smallest number may be the most negative one.
For example, if nums = [10,5,8,3,12], the answer is 3. If nums = [0,-1,-5,2], the answer is -5. If the array has only one element, that element is both the smallest and the largest.
So the task is to inspect the array and return the minimum value it contains.
Example Input & Output
Example 1: The smallest value in [10, 5, 8, 3, 12] is 3
Example 2: The smallest value in [0, -1, -5, 2] is -5
Example 3: With a single element, that element is the smallest
Algorithm Flow

Solution Approach
A single scan is enough here. We keep track of the smallest value seen so far and update it whenever we find something even smaller.
Start from the first element:
Then compare the rest of the array against it:
Every time a smaller number appears, we replace minValue. After the loop, it holds the minimum value in the whole array.
Then return it:
This solution runs in O(n) time and uses O(1) extra space.
Best Answers
class Solution {
public int find_smallest(int[] nums) {
if (nums == null || nums.length == 0) return 0;
int min = nums[0];
for (int num : nums) {
if (num < min) min = num;
}
return min;
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
