Longest Arithmetic Sequence After Changing At Most One Element

You are given an integer array nums.

A subarray is arithmetic if the difference between consecutive elements in the subarray is constant.

You can replace at most one element in nums with any integer. Then, you select an arithmetic subarray from nums.

Return an integer denoting the maximum length of the arithmetic subarray you can select.

Example 1
Inputnums = [9,7,5,10,1]
Output5
Replacing nums[3] = 10 with 3 makes the entire array [9, 7, 5, 3, 1] arithmetic with common difference -2.
Example 2
Inputnums = [1,2,6,7]
Output3
Replacing nums[0] = 1 with -2 makes [-2, 2, 6] an arithmetic subarray with common difference 4.

Constraints

  • 4 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^5

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate