Skip to content

Commit 4ad5b28

Browse files
authored
Create 1630-arithmetic-subarrays.js
1 parent d280561 commit 4ad5b28

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

1630-arithmetic-subarrays.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number[]} l
4+
* @param {number[]} r
5+
* @return {boolean[]}
6+
*/
7+
const checkArithmeticSubarrays = function(nums, l, r) {
8+
const len = l.length
9+
const res = []
10+
for(let i = 0; i < len; i++) {
11+
res.push(chk(nums.slice(l[i], r[i] + 1)))
12+
}
13+
return res
14+
};
15+
16+
function chk(arr) {
17+
if(arr.length === 0 || arr.length === 1 || arr.length === 2) return true
18+
arr.sort((a, b) => a - b)
19+
const diff = arr[1] - arr[0]
20+
for(let i = 2, len = arr.length; i < len; i++) {
21+
if(arr[i] - arr[i - 1] !== diff) return false
22+
}
23+
return true
24+
}

0 commit comments

Comments
 (0)