Code Logo

Find Minimum

Published at25 Jul 2026
Statistics Easy 6 views
Like0

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

Example 1
Input
[10,20,30]
Output
10
Explanation

First element is min

Example 2
Input
[3,1,4,1,5]
Output
1
Explanation

min of [3,1,4,1,5]

Example 3
Input
[5]
Output
5
Explanation

Single element

Example 4
Input
[-5,-2,-10,-1]
Output
-10
Explanation

Negative numbers

Example 5
Input
[9,7,5,3,1]
Output
1
Explanation

Descending order

Algorithm Flow

Recommendation Algorithm Flow for Find Minimum
Recommendation Algorithm Flow for Find Minimum

Solution Approach

Iterate through the array, tracking the smallest value seen so far.

function solution(arr) {
  if (arr.length === 0) return 0;
  var min = arr[0];
  for (var i = 1; i < arr.length; i++) {
    if (arr[i] < min) min = arr[i];
  }
  return min;
}

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

java
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;
    }
}