Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
Given a 0-indexed integer array nums of size n, find the maximum difference between nums[i] and nums[j] (i.e., nums[j] - nums[i]), such that 0 <= i < j < n and nums[i] < nums[j].
Return the maximum difference. If no such i and j exists, return -1.
Example 1:
Input: nums = [7,1,5,4] Output: 4 Explanation: The maximum difference occurs with i = 1 and j = 2, nums[j] - nums[i] = 5 - 1 = 4. Note that with i = 1 and j = 0, the difference nums[j] - nums[i] = 7 - 1 = 6, but i > j, so it is not valid.
Example 2:
Input: nums = [9,4,3,2] Output: -1 Explanation: There is no i and j such that i < j and nums[i] < nums[j].
Example 3:
Input: nums = [1,5,2,10] Output: 9 Explanation: The maximum difference occurs with i = 0 and j = 3, nums[j] - nums[i] = 10 - 1 = 9.
Constraints:
n == nums.length2 <= n <= 10001 <= nums[i] <= 109Problem summary: Given a 0-indexed integer array nums of size n, find the maximum difference between nums[i] and nums[j] (i.e., nums[j] - nums[i]), such that 0 <= i < j < n and nums[i] < nums[j]. Return the maximum difference. If no such i and j exists, return -1.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[7,1,5,4]
[9,4,3,2]
[1,5,2,10]
best-time-to-buy-and-sell-stock)two-furthest-houses-with-different-colors)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2016: Maximum Difference Between Increasing Elements
class Solution {
public int maximumDifference(int[] nums) {
int mi = 1 << 30;
int ans = -1;
for (int x : nums) {
if (x > mi) {
ans = Math.max(ans, x - mi);
} else {
mi = x;
}
}
return ans;
}
}
// Accepted solution for LeetCode #2016: Maximum Difference Between Increasing Elements
func maximumDifference(nums []int) int {
mi := 1 << 30
ans := -1
for _, x := range nums {
if mi < x {
ans = max(ans, x-mi)
} else {
mi = x
}
}
return ans
}
# Accepted solution for LeetCode #2016: Maximum Difference Between Increasing Elements
class Solution:
def maximumDifference(self, nums: List[int]) -> int:
mi = inf
ans = -1
for x in nums:
if x > mi:
ans = max(ans, x - mi)
else:
mi = x
return ans
// Accepted solution for LeetCode #2016: Maximum Difference Between Increasing Elements
impl Solution {
pub fn maximum_difference(nums: Vec<i32>) -> i32 {
let mut mi = i32::MAX;
let mut ans = -1;
for &x in &nums {
if x > mi {
ans = ans.max(x - mi);
} else {
mi = x;
}
}
ans
}
}
// Accepted solution for LeetCode #2016: Maximum Difference Between Increasing Elements
function maximumDifference(nums: number[]): number {
let [ans, mi] = [-1, Infinity];
for (const x of nums) {
if (x > mi) {
ans = Math.max(ans, x - mi);
} else {
mi = x;
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.