Problem · Array
Maximum Forward Difference
Learn this problemProblem statement
Return the maximum nums[j] - nums[i] over indices i < j with nums[i] <= nums[j]. Return -1 when no pair exists.
Function
maximumDifference(nums: int[]) → intExamples
Example 1
nums = [7,2,3,10,1]return = 8Choose 2 before 10; the later 1 cannot be paired backward with 10.
Constraints
1 <= nums.length <= 100000- Values fit in a signed 32-bit integer and the result fits in
int.