Longest Arithmetic Subarray After One Change
Updated
Problem statement
You are given an integer array deviation.
You may change at most one element to any integer you like, or change nothing. After that, find the longest subarray that forms an arithmetic progression, and return its length.
The changed element stays in its place. It can link the unchanged elements on its left and on its right into one longer arithmetic subarray.
A subarray is a run of elements that sit next to each other in the array. It forms an arithmetic progression when each element minus the one just before it gives the same value all the way through.
Function
longestArithmeticSubarrayAfterOneChange(deviation: int[]) → intExamples
Example 1
deviation = [8, 5, 2, 1, 100]return = 4Change the 1 to -1. The start of the array becomes [8,5,2,-1], and each step in it is -3. That run is arithmetic, so the answer is its length, 4.
Example 2
deviation = [1, 2, 3, 4, 100, 6, 7, 8, 9, 10]return = 10Change the 100 to 5. Now the whole array goes up by 1 at every step, so the answer is the length of the whole array.
Constraints
1 <= deviation.length <= 105-109 <= deviation[i] <= 109- You may change at most one element, and the new value can be any integer.
More Amazon Online Assessment questions
- Maximize K-Element AND with an Increment BudgetOA · Seen Nov 2026
- Task SchedulerOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Top K Frequent ElementsOA · PHONE SCREEN · Seen Nov 2026
- Get Smallest Base SegmentOA · Seen Nov 2026
- Minimum Cost of Left and Right PropagationOA · Seen Nov 2026
- Minimum Grid InconvenienceOA · Seen Nov 2026
- Maximize Shipment Order by Sorting One WindowOA · Seen Nov 2026
- Maximum Equal-Bit Pairs in a SubsequenceOA · Seen Nov 2026
- First Non-Repeating CharacterOA · ONSITE INTERVIEW · Seen Nov 2026
- Two SumOA · PHONE SCREEN · Seen Nov 2026