Code Logo

Find Warmest Temperature

Published at19 Apr 2026
Searching & Sorting Easy 8 views
Like0

Given an array of daily temperature readings, find the warmest (highest) temperature. Return the maximum value from the list. If the array is empty, return 0.

For example, the warmest from [22, 31, 28, 25, 19] is 31 degrees. From [-5, 0, 10] it is 10. From [100] it is 100. An empty array returns 0.

Finding the maximum temperature is a classic linear scan problem. It is used in weather data analysis, climate monitoring, and sensor data processing. The pattern of tracking a running maximum by comparing each value against the current best is fundamental to many algorithms.

The solution initializes max to the first element, then iterates through remaining values. If a value exceeds max, update it. After the loop, return max. This runs in O(n) time with O(1) space.

Edge cases include an empty array (return 0), a single temperature (return it), all equal temperatures (return that value), and negative temperatures (correctly identified as the maximum).

Learn about our pseudocode specification
Guide

Example Input & Output

Example 1
Input
temps = [28, 31, 26, 33, 30]
Output
33
Explanation

33 is the highest reading in the list.

Example 2
Input
temps = [15]
Output
15
Explanation

With one reading, that value is automatically the warmest.

Example 3
Input
temps = []
Output
-1
Explanation

An empty list has no valid temperature to return.

Algorithm Flow

Recommendation Algorithm Flow for Find Warmest Temperature

Solution Approach

Iterate through the array while tracking the highest temperature seen so far.

function warmest(temps)
  if temps is empty then return 0
  max = temps[0]
  for each t in temps
    if t > max then max = t
  return max

Handle the empty case first by returning 0. Initialize max to the first temperature. Loop through each temperature; if the current value is greater than max, update max. Return max after all values have been examined.

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

Best Answers

Pseudocode - Approach 1
program find_warmest_temperature
dictionary
   temps: array[1..100] of integer
   warmest, i, n: integer
algorithm
   input(temps)
   n <- temps.length
   if n = 0 then
      output(-1)
   else
      warmest <- temps[0]
      for i <- 1 to n - 1 do
         if temps[i] > warmest then
            warmest <- temps[i]
         endif
      endfor
      output(warmest)
   endif
endprogram