# Maximum Difference Between Increasing Elements (LeetCode 2016)

Max nums[j]-nums[i] with j>i and nums[j]>nums[i], else -1.

## Approach

```python
def maximumDifference(nums):
    ans, mn = -1, nums[0]
    for x in nums[1:]:
        if x > mn:
            ans = max(ans, x - mn)
        else:
            mn = x
    return ans
```

## Complexity

- Time / space depend on the scan above; keep the solution linear when possible.

## Interview tip

Narrate the invariant before coding. I drill these patterns with [AI interview assistant](https://www.linkjob.ai/interview-copilot/) ahead of OA rounds.

*Disclosure: I work on Linkjob AI, an interview copilot. The algorithm above stands on its own.*
