Maximum Later Difference
Updated
Problem statement
Given nums, return the largest difference nums[j] - nums[i] over indices i < j with nums[j] > nums[i].
If no such pair exists, return the no-pair value -1. Equal values do not form a valid pair.
Function
maximumLaterDifference(nums: int[]) → intExamples
Example 1
nums = [3,6,10,1,4,6,5]return = 7Ten comes after three, and no other pair has a larger difference, so the answer is seven.
Example 2
nums = [5,5,4]return = -1Each later value is equal to or smaller than every earlier one, so no valid pair exists and the answer is the no-pair value.
Constraints
1 <= nums.length <= 10^5.-10^9 <= nums[i] <= 10^9.
More Apple interview questions
- Merge IntervalsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Merge Two Strings by Maximum Boundary OverlapOA · Seen Sep 2026
- Collect Letters Along a Unique Grid PathOA · Seen Aug 2025
- Maximum Weight in Two BagsOA · Seen Aug 2025
- Ignore SectionsOA · Seen Mar 2024
- Gas Station Circular TourOA · Seen Nov 2021
- House Robber IIOA · Seen Nov 2021
- Clone an Undirected Graph StateOA · Seen Mar 2021
- Longest Increasing Subsequence LengthOA · Seen Feb 2021
- Topological OrderingPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Catch Fish with Reusable BaitsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Server InvestmentRippling · OARippling interview questions
- Book Reading EveningsGoogle · OAGoogle interview questions
- Maximum Apples That Fit in a BoxVirtu Financial · OAVirtu Financial interview questions
- Minimum Project Bid CostNVIDIA · OANVIDIA interview questions
- Minimum AmountBelvedere Trading · OA
- Maximum Income from Asset TradingJefferies · OA
- Maximum Three-Server Difficulty ScoreVisa · OAVisa interview questions
- Economical TripAccenture · OAAccenture interview questions
- Best Time to Buy and Sell StockFlexTrade · OAFlexTrade interview questions