Find Highest Score
Given an array of scores, find the highest score. Return the maximum value from the list. If the array is empty, return 0.
For example, the highest score from [45, 78, 92, 33, 67] is 92. From [100] it is 100. From [10, 20, 30] it is 30. An empty array returns 0.
Finding the maximum value is a mirror image of finding the minimum and is equally fundamental. The same linear scan pattern applies: initialize a candidate with a low sentinel value (or the first element), iterate through all elements, and update whenever a larger value is found.
This algorithm is the building block for more complex operations like finding the top K elements, computing the range (max - min), and normalizing data to a 0-1 scale. It appears in nearly every domain that processes collections of numerical data.
The solution iterates through the array keeping track of the largest value seen so far. If the current score exceeds the stored maximum, update it. After the loop, return the maximum. Time complexity is O(n), space complexity is O(1). Edge cases include an empty array (return 0), a single element (return it), and all equal scores (return that value).
Example Input & Output
91 is the highest score in the list.
With one score, that value is automatically the highest.
An empty list has no valid score to return.
Algorithm Flow
Solution Approach
Iterate through the list and track the highest score seen so far.
First handle the empty list case by returning 0. Then initialize max to the first score. Loop through each remaining score; if the current score is larger than max, update max. After the loop, return max. This ensures the highest score is correctly identified regardless of its position in the list.
The algorithm is O(n) time with O(1) space. It is the mirror image of finding the minimum and uses the same linear scan pattern. This fundamental technique is the basis for more complex operations like finding the top K elements, computing statistical ranges, and normalizing data sets.
Best Answers
program find_highest_score
dictionary
scores: array[1..100] of integer
highest, i, n: integer
algorithm
input(scores)
n <- scores.length
if n = 0 then
output(-1)
else
highest <- scores[0]
for i <- 1 to n - 1 do
if scores[i] > highest then
highest <- scores[i]
endif
endfor
output(highest)
endif
endprogramComments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
