How to find the Largest Difference in an Array
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
The phrase "largest difference in an array" often means finding the maximum value of arr[j] - arr[i] where j > i. That order condition matters because the task is usually about a later value minus an earlier value, not simply max(array) - min(array).
Clarify The Problem First
There are two common versions of this problem.
- ordered version: the larger element must appear after the smaller one
- unordered version: any two elements are allowed, so the answer is just
max(array) - min(array)
Most interview-style questions mean the ordered version, which is the same idea as maximum single-transaction stock profit.
Brute Force Solution
The direct approach is to check every valid pair.
This works, but it takes O(n^2) time because it considers every pair.
Linear-Time Solution
A better solution scans the array once, keeping track of the smallest value seen so far and the best difference found so far.
The idea is simple: at each position, ask what profit or difference would be achieved if you paired the current value with the smallest earlier value.
Walk Through An Example
For the array [2, 3, 10, 6, 4, 8, 1]:
- start with
min_so_far = 2 - at
3, difference is1 - at
10, difference is8, which becomes the current best - later values do not beat
8
So the answer is 8, from 10 - 2.
What If The Array Is Decreasing
The algorithm still works even if every later value is smaller. For [9, 7, 4, 1], the best ordered difference is negative because every valid pair loses value.
That behavior is often correct. If the problem statement instead wants 0 when no positive gain exists, clamp the result with max(best, 0).
Space And Time Complexity
The optimized algorithm takes:
- time:
O(n) - extra space:
O(1)
That is optimal for a single left-to-right scan.
Related Variant: Absolute Largest Gap
If order does not matter, the answer is much simpler.
This is a different problem, so it is worth checking the requirement before implementing the algorithm.
Common Pitfalls
A common mistake is returning max(nums) - min(nums) for the ordered version. That can be wrong if the minimum occurs after the maximum.
Another mistake is initializing the best value to 0, which hides valid negative answers for strictly decreasing arrays.
It is also easy to forget edge cases such as empty arrays or single-element arrays. Those inputs do not contain a valid pair, so the function should raise an error or return a documented sentinel value.
Summary
- The ordered version asks for the maximum
arr[j] - arr[i]withj > i. - A one-pass scan with
min_so_farsolves it inO(n)time. - Do not confuse the ordered version with simple
max - min. - Decide whether negative answers are allowed or should be clamped to
0. - Handle arrays with fewer than two elements explicitly.
Related reading
- how to find the least number of operations to compute xn
- How to find the length of a linked list that is having cycles in it?
- How to find the lexicographically smallest string by reversing a substring?
- How to find the lowest common ancestor of two nodes in any binary tree?
- How to find the max distance between a set of nodes on a tree?
- How to find the minimum number of moves to move an item into a position in a stack?
- How to find the minimum positive number K for an array to make the array in strictly ascending order
- How to find the most likely sequences of hidden states for a Hidden Markov Model

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.