Find Maximum Difference
Updated
Problem statement
You are given a list of n integers, inputArray. Find the largest value of inputArray[j] - inputArray[i] where i < j and inputArray[j] > inputArray[i]. In other words, the larger number must come after the smaller one.
If no such pair exists, return 0.
Function
findMaximumDifference(n: int, inputArray: List<Integer>) → intExamples
Example 1
n = 7inputArray = [2, 3, 10, 6, 4, 8, 1]return = 8The best pair is 2 followed later by 10, giving 10 - 2 = 8. The pair 10 and 1 does not count, because the larger number 10 comes before the smaller one. So the output is 8.
Example 2
n = 3inputArray = [4, 3, 1]return = 0The list only goes down: every later number is smaller than every earlier one. No pair has the larger number after the smaller one, so the output is 0.
Constraints
1 ≤ n ≤ 10^5inputArray.length == n0 ≤ inputArray[i] ≤ 10^9
More Cisco interview questions
- Check if a String Is a PalindromePHONE SCREEN · Seen Nov 2026
- Find Unique ElementsPHONE SCREEN · Seen Nov 2026
- First Letter to Appear TwicePHONE SCREEN · Seen Nov 2026
- Middle of a Linked ListPHONE SCREEN · Seen Nov 2026
- Reverse a Linked ListPHONE SCREEN · Seen Nov 2026
- Reverse a StringPHONE SCREEN · Seen Nov 2026
- Valid ParenthesesPHONE SCREEN · Seen Nov 2026
- K-Step Bit Flip with RetentionPHONE SCREEN · Seen Nov 2026
- Todo Task Manager OperationsONSITE INTERVIEW · Seen Oct 2026
- Find the Duplicate Number Without Modifying the ArrayONSITE INTERVIEW · Seen Sep 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