Find Warmest Temperature
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).
Example Input & Output
33 is the highest reading in the list.
With one reading, that value is automatically the warmest.
An empty list has no valid temperature to return.
Algorithm Flow
Solution Approach
Iterate through the array while tracking the highest temperature seen so far.
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
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
endprogramComments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
