Code Logo

Priority Queue Min

Published at25 Jul 2026
Priority Queue Easy 0 views
Like0

Given an array representing a priority queue (min-heap), return the minimum element without removing it. In a min-heap priority queue, the smallest element is always at the root (index 0).

For example, the minimum of [1, 3, 5, 7, 9] is 1. The minimum of [-5, 0, 10] is -5. If the array is empty, return 0. The operation does not modify the priority queue.

Peeking at the minimum element is a fundamental priority queue operation. It allows you to inspect the highest-priority (smallest) element without actually removing it. This is useful when you need to conditionally process the next item or when multiple queues need to be compared by their minimum values.

In a min-heap, the root at index 0 is always the smallest element. This is guaranteed by the heap invariant: every parent node is smaller than or equal to its children. Therefore, peeking at the minimum is an O(1) operation — just return the first element of the array.

Edge cases include an empty priority queue (return 0), a single-element queue (return that element), and a queue with all equal values (return that value). The heap structure ensures the minimum is always at the root regardless of how many elements have been inserted or extracted.

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

Solution Approach

Return the first element of the array, which is the root of the min-heap.

function solution(arr) {
  return arr.length > 0 ? arr[0] : 0;
}

In a min-heap array, the smallest element is always at index 0 (the root). Return it if the array is not empty. If the array is empty, return 0 as a sentinel value. This works because the heap invariant guarantees that every parent is smaller than its children, so the minimum bubbles up to the root.

Time complexity is O(1), space complexity is O(1).

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