Code Logo

Priority Queue Min

Published at25 Jul 2026
Priority Queue Easy 0 views
Like0

Find the minimum value in an array (simulating peeking at the smallest element in a min-priority queue). Return -1 if empty.

Example Input & Output

Example 1
Input
[10,5,7]
Output
5
Explanation

Min is 5

Example 2
Input
[42]
Output
42
Explanation

Single element

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

Min is 1

Example 4
Input
[]
Output
-1
Explanation

Empty

Example 5
Input
[-5,0,3]
Output
-5
Explanation

Negative min

Algorithm Flow

Recommendation Algorithm Flow for Priority Queue Min
Recommendation Algorithm Flow for Priority Queue Min

Solution Approach

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

Best Answers

java
class Solution {
    public int solution(int[] nums) {
        if(nums.length==0)return -1;int m=nums[0];for(int n:nums){if(n<m)m=n;}return m;
    }
}