-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path23.js
45 lines (37 loc) · 842 Bytes
/
23.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
* @param {number[]} nums
* @param {number[]} l
* @param {number[]} r
* @return {boolean[]}
*/
var checkArithmeticSubarrays = function(nums, l, r) {
let result = [];
for (let i = 0; i < l.length; i++) {
result.push(isArithmetic(nums.slice(l[i], r[i] + 1)));
}
return result;
};
function isArithmetic(arr) {
let min = Math.min(...arr);
let max = Math.max(...arr);
if (min == max) {
return true;
}
let step = (max - min) / (arr.length - 1);
if (step != Math.floor(step)) {
return false;
}
let set = new Set();
for (let x of arr) {
if (set.has(x)) {
return false;
}
set.add(x);
}
for (let x = min; x <= max; x += step) {
if (!set.has(x)) {
return false;
}
}
return true;
}