We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent d280561 commit 4ad5b28Copy full SHA for 4ad5b28
1630-arithmetic-subarrays.js
@@ -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