array
maximum difference
absolute value
algorithm
programming

Maximum absolute difference in an array

Master System Design with Codemia

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

Introduction

The maximum absolute difference between two elements in an array is simpler than it first sounds. You do not need to compare every pair. The largest absolute difference is always the distance between the array's minimum value and maximum value.

Why the answer is max - min

For any two values a and b, the absolute difference is:

text
|a - b|

To make that as large as possible, choose the smallest and largest elements available. No other pair can produce a wider spread than the extremes of the array.

So the problem reduces to:

  • find the minimum element
  • find the maximum element
  • subtract

Linear-time solution

That gives an O(n) algorithm with constant extra space.

python
1def max_absolute_difference(arr):
2    if not arr:
3        raise ValueError("array must not be empty")
4
5    min_val = arr[0]
6    max_val = arr[0]
7
8    for value in arr[1:]:
9        if value < min_val:
10            min_val = value
11        if value > max_val:
12            max_val = value
13
14    return max_val - min_val
15
16print(max_absolute_difference([3, 10, -4, 7]))

The result here is 14, coming from 10 - (-4).

Why brute force is unnecessary

A naive solution checks every pair of elements:

python
1def brute_force(arr):
2    best = 0
3    for i in range(len(arr)):
4        for j in range(len(arr)):
5            best = max(best, abs(arr[i] - arr[j]))
6    return best

That works, but it is O(n^2), which is unnecessary once you notice that only the minimum and maximum matter.

Edge cases

A few cases are worth handling explicitly:

  • empty array: no valid answer
  • one element: maximum absolute difference is 0
  • duplicate values: still fine, because min and max may be equal

Example:

python
print(max_absolute_difference([5]))          # 0
print(max_absolute_difference([2, 2, 2]))    # 0
print(max_absolute_difference([-5, -1, -9])) # 8

The formula still holds.

Built-in helpers are fine too

In Python, the shortest correct solution is:

python
1def max_absolute_difference(arr):
2    if not arr:
3        raise ValueError("array must not be empty")
4    return max(arr) - min(arr)

This is still linear time because max and min each scan the array once.

If you are in an interview or a low-level language without convenient built-ins, the explicit one-pass loop is often better because it shows the underlying reasoning.

Returning the pair as well as the value

If you need not only the maximum difference but also the elements that produce it, return the minimum and maximum values alongside the difference. The same scan that finds the answer can also keep those two values without changing the overall time complexity.

Be careful about problem variants

Some problems sound similar but are different, such as:

  • maximize |A[i] - A[j]| + |i - j|
  • require i < j
  • ask for the pair itself rather than only the value

For the plain maximum absolute difference in an array, max - min is enough. For the variants, the solution can change.

Common Pitfalls

  • Using a nested-loop brute-force solution when only min and max matter.
  • Forgetting to handle the empty-array case.
  • Confusing this problem with harder indexed variants involving i and j.
  • Returning abs(max - min) even though max - min is already non-negative.
  • Overcomplicating the solution when the array extremes already contain the answer.

Summary

  • The maximum absolute difference in an array is always max(arr) - min(arr).
  • You only need the minimum and maximum elements, not every pair.
  • The optimal algorithm is linear time with constant extra space.
  • Handle empty arrays explicitly and single-element arrays naturally return 0.
  • Watch out for similarly worded variants that add index terms or ordering constraints.

Course illustration
Course illustration

All Rights Reserved.