-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathArithmeticSlices.cpp
47 lines (47 loc) · 1.18 KB
/
ArithmeticSlices.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
46
47
class Solution {
public:
int numberOfArithmeticSlices(vector<int>& A) {
int sz=A.size(),left,right,second,d,con=0,k,flag;
for(left=0;left<sz-2;left++)
{
second=left+1;
d=A[second]-A[left];
for(right=left+2;right<sz;right++)
{
flag=0;
for(k=left+1;k<=right;k++)
{
if(A[k]-A[k-1]!=d)
{
flag=1;
break;
}
}
if(!flag)
con++;
}
}
for(left=0;left<sz-2;left++)
{
second=left+1;
d=A[second]-A[left];
for(right=left+2;right<sz;right++)
{
if(A[right]-A[right-1]!=d)
break;
else
con++;
}
}
vector<int>dp(sz+1,0);
for(left=2;left<sz;left++)
{
if(A[left]-A[left-1] == A[left-1]-A[left-2])
{
dp[left]=1+dp[left-1];
con=con+dp[left];
}
}
return con;
}
};