-
Notifications
You must be signed in to change notification settings - Fork 0
/
Arithmetic Subarrays
35 lines (33 loc) · 971 Bytes
/
Arithmetic Subarrays
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
// Runtime: 88 ms, faster than 59.80% of C++ online submissions for Arithmetic Subarrays.
// Memory Usage: 43.5 MB, less than 11.40% of C++ online submissions for Arithmetic Subarrays.
class Solution {
public:
bool check_AP(vector<int>v)
{
sort(v.begin(),v.end());
int diff=v[1]-v[0];
for(int i=1;i<v.size();i++)
{
if(v[i]-v[i-1]!=diff)
{
return false;
}
}
return true;
}
vector<bool> checkArithmeticSubarrays(vector<int>& nums, vector<int>& l, vector<int>& r) {
vector<bool> ans;
for(int i=0;i<l.size();i++)
{
int start=l[i],end=r[i];
int f=0;
vector<int> res;
for(int i=start;i<=end;i++){
res.push_back(nums[i]);
}
if(check_AP(res)) ans.push_back(true);
else ans.push_back(false);
}
return ans;
}
};