Problem
Longest Arithmetic Subarray After One Change
Learn this problemProblem statement
You are given an integer array deviation.
You may change at most one element of the array to any integer value. After making at most one change, find the maximum possible length of a contiguous subarray that forms an arithmetic progression.
The changed element stays at its original index and may be used to connect the unchanged elements before it and after it into one longer arithmetic subarray.
A contiguous subarray forms an arithmetic progression if the difference between every pair of consecutive elements in that subarray is the same.
Function
longestArithmeticSubarrayAfterOneChange(deviation: int[]) → intExamples
Example 1
deviation = [8, 5, 2, 1, 100]return = 4Change 1 to -1. The contiguous subarray [8,5,2,-1] has common difference -3, so its length is 4.
Example 2
deviation = [1, 2, 3, 4, 100, 6, 7, 8, 9, 10]return = 10Change 100 to 5. The entire array becomes an arithmetic progression with common difference 1.
Constraints
Constraints:
1 <= deviation.length <= 105-109 <= deviation[i] <= 109- You may change at most one element, and the changed value may be any integer.
More Amazon problems
- HTTP Request RedirectionOA · Seen Jul 2026
- Merge IntervalsOA · Seen Jul 2026
- Permutation SorterOA · Seen Jul 2026
- Resolve Task DependenciesONSITE INTERVIEW · Seen Jul 2026
- Shortest Distance on a Circular Bus RouteOA · Seen Jul 2026
- Longest Increasing Subsequence With Bounded Adjacent DifferenceONSITE INTERVIEW · Seen Jul 2026
- Search in a Rotated Sorted ArrayONSITE INTERVIEW · Seen Jul 2026
- Sliding Window MaximumONSITE INTERVIEW · Seen Jul 2026