Code Logo

Find Maximum Value (Pseudocode)

Published at19 Apr 2026
Searching & Sorting Easy 8 views
Like0

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).

Learn about our pseudocode specification
Guide

Algorithm Flow

Recommendation Algorithm Flow for Find Maximum Value (Pseudocode)
Recommendation Algorithm Flow for Find Maximum Value (Pseudocode)

Solution Approach

Iterate through the array while tracking the largest value seen so far.

function findMax(arr)
  if arr is empty then return 0
  max = arr[0]
  for each val in arr
    if val > max then max = val
  return max

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

Pseudocode - Approach 1
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)
endprogram