Find Maximum Value (Pseudocode)
Given an array of integers, find the maximum value. Return the largest element in the array. If the array is empty, return 0.
For example, the maximum of [3, 7, 2, 9, 5] is 9. The maximum of [-5, -2, -10] is -2. The maximum of [100] is 100. An empty array returns 0.
Finding the maximum value is one of the most fundamental operations on collections. It is used in data analysis (highest score, peak temperature), algorithm design (tournament selection, priority queues), and everyday computing (finding the largest file, the most expensive product).
The solution initializes max to the first element, then iterates through the remaining elements. If an element exceeds the current max, update max. 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 element (return that element), all equal values (return that value), and negative numbers (the maximum is the least negative value).
Algorithm Flow

Solution Approach
Iterate through the array while tracking the largest value seen so far.
Handle the empty case by returning 0. Initialize max to the first element. Loop through each value; if the current value is greater than max, update max. Return max after all elements have been examined.
Time complexity is O(n), space complexity is O(1).
Best Answers
program find_max
dictionary
n, i, max_val: integer
arr: array[1..100] of integer
algorithm
input(n)
for i <- 1 to n do
input(arr[i])
endfor
max_val <- arr[1]
for i <- 2 to n do
if arr[i] > max_val then
max_val <- arr[i]
endif
endfor
output(max_val)
endprogramComments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
