Given an integer array nums, choose two indices i < j so that the difference nums[j] - nums[i] is as large as possible.
Return that maximum difference. If no pair of indices gives a positive difference (the values never rise), return 0.
Examples
Example 1
Input: nums = [7,1,5,3,6,4]
Output: 5
Explanation: Take i = 1 (value 1) and j = 4 (value 6): 6 - 1 = 5. The pair must be in order (value 1 followed later by value 6), so 7 - 1 is not attainable.
Example 2
Input: nums = [7,6,4,3,1]
Output: 0
Explanation: The values only decrease, so no in-order pair has a positive difference.
Constraints
1≤nums.length≤105
0≤nums[i]≤104
Examples
Example 1
Input
nums = [7, 1, 5, 3, 6, 4]
Output
5
Example 2
Input
nums = [7, 6, 4, 3, 1]
Output
0
Loading editor...
Click "Run" to test with sample cases or "Submit" to run all tests.