Maximum Difference Between Increasing Elements

Given a 0-indexed integer array nums of size n, find the maximum value of nums[j] - nums[i] over all pairs 0 <= i < j < n with nums[i] < nums[j]. Return that maximum, or -1 if no such pair exists.

30:00

Maximum Difference Between Increasing Elements
easy
Free written solution
Topics
Companies

Given a 0-indexed integer array nums of size n, find the maximum value of nums[j] - nums[i] over all pairs 0 <= i < j < n with nums[i] < nums[j]. Return that maximum, or -1 if no such pair exists.

Example 1:
Input: {"nums":[7,1,5,4]}
Output: 4
Constraints:
  • n=nums.lengthn = \text{nums.length}

  • 2≤n≤10002 \leq n \leq 1000

  • 1≤nums[i]≤1091 \leq \text{nums}[i] \leq 10^9

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums":[7,1,5,4]}

best starts at -1, meaning no increasing pair has been found yet.

nums as heights

7

1

5

4

0

1

2

3

low

7

nums[0]

current

-

best

-1

no rise yet

Variables
VariableValue
best-1
DepthFunction Call
Stack empty
0/5