Skip to content

Commit cad7a21

Browse files
committed
feat: solve No.1630
1 parent be80bc0 commit cad7a21

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed
+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# 1630. Arithmetic Subarrays
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Array, Sorting.
5+
- Similar Questions: Arithmetic Slices, Can Make Arithmetic Progression From Sequence.
6+
7+
## Problem
8+
9+
A sequence of numbers is called **arithmetic** if it consists of at least two elements, and the difference between every two consecutive elements is the same. More formally, a sequence `s` is arithmetic if and only if `s[i+1] - s[i] == s[1] - s[0] `for all valid `i`.
10+
11+
For example, these are **arithmetic** sequences:
12+
13+
```
14+
1, 3, 5, 7, 9
15+
7, 7, 7, 7
16+
3, -1, -5, -9
17+
```
18+
19+
The following sequence is not **arithmetic**:
20+
21+
```
22+
1, 1, 2, 5, 7
23+
```
24+
25+
You are given an array of `n` integers, `nums`, and two arrays of `m` integers each, `l` and `r`, representing the `m` range queries, where the `ith` query is the range `[l[i], r[i]]`. All the arrays are **0-indexed**.
26+
27+
Return **a list of **`boolean` **elements** `answer`**, where** `answer[i]` **is** `true` **if the subarray** `nums[l[i]], nums[l[i]+1], ... , nums[r[i]]`** can be **rearranged** to form an **arithmetic** sequence, and** `false` **otherwise.**
28+
29+
 
30+
Example 1:
31+
32+
```
33+
Input: nums = [4,6,5,9,3,7], l = [0,0,2], r = [2,3,5]
34+
Output: [true,false,true]
35+
Explanation:
36+
In the 0th query, the subarray is [4,6,5]. This can be rearranged as [6,5,4], which is an arithmetic sequence.
37+
In the 1st query, the subarray is [4,6,5,9]. This cannot be rearranged as an arithmetic sequence.
38+
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.
39+
```
40+
41+
Example 2:
42+
43+
```
44+
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]
45+
Output: [false,true,false,false,true,true]
46+
```
47+
48+
 
49+
**Constraints:**
50+
51+
52+
53+
- `n == nums.length`
54+
55+
- `m == l.length`
56+
57+
- `m == r.length`
58+
59+
- `2 <= n <= 500`
60+
61+
- `1 <= m <= 500`
62+
63+
- `0 <= l[i] < r[i] < n`
64+
65+
- `-105 <= nums[i] <= 105`
66+
67+
68+
69+
## Solution
70+
71+
```javascript
72+
/**
73+
* @param {number[]} nums
74+
* @param {number[]} l
75+
* @param {number[]} r
76+
* @return {boolean[]}
77+
*/
78+
var checkArithmeticSubarrays = function(nums, l, r) {
79+
return l.map((_, i) => check(nums, l[i], r[i]));
80+
};
81+
82+
var check = function(nums, l, r) {
83+
var map = {};
84+
var min = Number.MAX_SAFE_INTEGER;
85+
var max = Number.MIN_SAFE_INTEGER;
86+
for (var i = l; i <= r; i++) {
87+
min = Math.min(min, nums[i]);
88+
max = Math.max(max, nums[i]);
89+
map[nums[i]] = true;
90+
}
91+
var diff = (max - min) / (r - l);
92+
for (var num = min; num < max; num += diff) {
93+
if (map[num] === undefined) {
94+
return false;
95+
}
96+
}
97+
return true;
98+
};
99+
```
100+
101+
**Explain:**
102+
103+
nope.
104+
105+
**Complexity:**
106+
107+
* Time complexity : O(n * m).
108+
* Space complexity : O(n).

0 commit comments

Comments
 (0)