recursion
algorithm
array
programming
coding

Find maximum value in an array by recursion

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Finding the maximum value in an array is a simple problem, which makes it a good recursion exercise. The recursive idea is to reduce the problem size one element at a time until only one element remains, then build the answer back up through comparisons.

The Recursive Idea

A recursive solution needs two parts:

  1. A base case.
  2. A recursive case.

For an array maximum, the base case is “the subarray has one element, so that element is the maximum.” The recursive case is “the maximum of the first n elements is the larger of the last element and the maximum of the first n - 1 elements.”

That translates naturally into code.

A Simple Python Implementation

python
1def recursive_max(values, n=None):
2    if not values:
3        raise ValueError("values must not be empty")
4
5    if n is None:
6        n = len(values)
7
8    if n == 1:
9        return values[0]
10
11    max_of_rest = recursive_max(values, n - 1)
12    return values[n - 1] if values[n - 1] > max_of_rest else max_of_rest
13
14
15numbers = [7, 3, 19, 4, 12]
16print(recursive_max(numbers))

Output:

text
19

The first call asks for the maximum of five elements. That calls the same function for four elements, then three, then two, then one. Once the base case returns, each stack frame compares its last element with the result below it.

A Version That Uses an Index

Some developers prefer passing an index instead of n because it makes the current position explicit:

python
1def recursive_max_from(values, index=0):
2    if not values:
3        raise ValueError("values must not be empty")
4
5    if index == len(values) - 1:
6        return values[index]
7
8    max_of_tail = recursive_max_from(values, index + 1)
9    return values[index] if values[index] > max_of_tail else max_of_tail
10
11
12print(recursive_max_from([2, 11, 5, 9]))

This version walks from left to right conceptually, while the first version shrinks the active prefix. Both have the same complexity.

Complexity and Practical Tradeoffs

The time complexity is O(n) because every element is visited once. The space complexity is also O(n) because every recursive call adds one stack frame.

That stack usage is why recursion is educational here, but not always the best production choice. An iterative loop solves the same problem with O(1) extra space:

python
1def iterative_max(values):
2    if not values:
3        raise ValueError("values must not be empty")
4
5    current = values[0]
6    for value in values[1:]:
7        if value > current:
8            current = value
9    return current

In Python especially, deep recursion can hit the recursion limit on large inputs, so an iterative version is usually the safer real-world choice.

How the Recursion Unfolds

For [7, 3, 19, 4], the call chain looks like this:

text
1max([7, 3, 19, 4])
2max([7, 3, 19])
3max([7, 3])
4max([7])

Then the comparisons happen while the calls return:

  • compare 3 with 7
  • compare 19 with 7
  • compare 4 with 19

That is the core recursive pattern: go down to the smallest solvable case, then combine partial answers on the way back.

Common Pitfalls

The most common mistake is forgetting to handle the empty array. Without an explicit guard, the recursion will either crash with an index error or recurse incorrectly.

Another issue is using the wrong base case. If you stop at n == 0 without carefully adjusting indexes, you may compare against invalid positions.

Developers also sometimes assume recursion is automatically more elegant or faster. For this problem, recursion is mainly a teaching tool. The iterative solution is simpler to run at scale.

Finally, be careful with very large arrays in Python. A correct recursive algorithm can still fail at runtime because of recursion-depth limits.

Summary

  • A recursive maximum function compares one element against the maximum of a smaller subproblem.
  • The base case is a one-element subarray.
  • The algorithm runs in O(n) time.
  • The recursive version uses O(n) call-stack space.
  • In production Python code, an iterative loop is usually the better default.

Course illustration
Course illustration

All Rights Reserved.