FastPrepMaximum Later Difference

Maximum Later Difference

Apple logoApple● EasyNEW GRADOA
Learn

Problem statement

Choose indices i < j in nums such that nums[j] > nums[i]. Return the largest possible difference nums[j] - nums[i].

Return -1 when no strictly positive later-minus-earlier difference exists. Equal values do not form a valid pair.

Function

maximumLaterDifference(nums: int[]) → int

Examples

Example 1

nums = [3,6,10,1,4,6,5]return = 7

The value ten appears after three, producing the source example's difference seven.

Example 2

nums = [5,5,4]return = -1

All later differences are zero or negative.

Constraints

  • 1 <= nums.length <= 10^5.
  • -10^9 <= nums[i] <= 10^9.

More Apple problems

See Apple hiring insights
public int maximumLaterDifference(int[] nums) {
    // Write your solution here.
}
nums[3,6,10,1,4,6,5]
expected7
Checking account…