-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArithmetic Subarrays.cpp
45 lines (45 loc) · 1.2 KB
/
Arithmetic Subarrays.cpp
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
class Solution {
public:
vector<bool> checkArithmeticSubarrays(vector<int>& nums, vector<int>& l, vector<int>& r) {
vector<bool>ans;
int n=nums.size();
int m=l.size();
for(int i=0;i<m;i++)
{
int x=l[i];int y=r[i];
vector<int>v;
for(int j=x;j<=y;j++)
v.push_back(nums[j]);
if(v.size()<2)
{
ans.push_back(false);
break;
}
else
{
sort(v.begin(),v.end());
if(v.size()==2)
{
ans.push_back(true);
}
else
{
int diff=v[1]-v[0];
int z=0;
for(int ind=2;ind<v.size();ind++)
{
if((v[ind]-v[ind-1])!=diff)
{
ans.push_back(false);
z=1;
break;
}
}
if(z==0)
ans.push_back(true);
}
}
}
return ans;
}
};