#1630

Arithmetic Subarrays

pupil · 410 · lc medium +25 · verified · 83.8% accepted · 1,896 likes · top 95%

Description

A sequence is arithmetic if all consecutive differences are equal. You are given integer array nums and query arrays l and r. For each query i, determine if the subarray nums[l[i]..r[i]] can be rearranged into an arithmetic sequence. Return a boolean list of results.

Example 1:

1, 3, 5, 7, 9
7, 7, 7, 7
3, -1, -5, -9

Example 2:

1, 1, 2, 5, 7

Example 3:

Input: nums = [4,6,5,9,3,7], l = [0,0,2], r = [2,3,5]
Output: [true,false,true]
Explanation:
In the 0th query, the subarray is [4,6,5]. This can be rearranged as [6,5,4], which is an arithmetic sequence.
In the 1st query, the subarray is [4,6,5,9]. This cannot be rearranged as an arithmetic sequence.
In the 2nd query, the subarray is [5,9,3,7]. This can be rearranged as [3,5,7,9], which is an arithmetic sequence.

Example 4:

Input: nums = [-12,-9,-3,-12,-6,15,20,-25,-20,-15,-10], l = [0,1,6,4,8,7], r = [4,4,9,7,9,10]
Output: [false,true,false,false,true,true]

Code

1
2
3