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.
Examples
01 · Example 1
deviation = [8, 5, 2, 1, 100] return = 4
Change 1 to -1. The contiguous subarray [8,5,2,-1] has common difference -3, so its length is 4.
02 · Example 2
deviation = [1, 2, 3, 4, 100, 6, 7, 8, 9, 10] return = 10
Change 100 to 5. The entire array becomes an arithmetic progression with common difference 1.
Constraints
1 <= deviation.length- You may change at most one element, and the changed value may be any integer.
More Amazon problems
- Count Promotional PeriodsOA · Seen Jun 2026
- Find Maximum Total Amount (SDE I, Fungible :)Seen Jun 2026
- Get Minimum AmountOA · Seen Jun 2026
- Find Minimum CostOA · Seen Jun 2026
- Get Smallest Base SegmentOA · Seen Jun 2026
- Maximum Non-Adjacent House ValueONSITE INTERVIEW · Seen Jun 2026
- Running Delivery Time MediansONSITE INTERVIEW · Seen Jun 2026
- Select Least Resource TasksOA · Seen Jun 2026
public int longestArithmeticSubarrayAfterOneChange(int[] deviation) {
// write your code here
}deviation[8, 5, 2, 1, 100]
expected4
sign in to submit