-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathArithmetic Slices.cpp
60 lines (46 loc) · 1.56 KB
/
Arithmetic Slices.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
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
Solution by Rahul Surana
***********************************************************
An integer array is called arithmetic if it consists of at least three elements and
if the difference between any two consecutive elements is the same.
For example, [1,3,5,7,9], [7,7,7,7], and [3,-1,-5,-9] are arithmetic sequences.
Given an integer array nums, return the number of arithmetic subarrays of nums.
A subarray is a contiguous subsequence of the array.
***********************************************************
*/
#include<bits/stdc++.h>
class Solution {
public:
int numberOfArithmeticSlices(vector<int>& nums) {
if(nums.size() < 3) return 0;
int d = nums[1]-nums[0];
int i = 0, j = 0;
int ans = 0;
while(j < nums.size()){
while(j<nums.size()-1 && j-i+1<3)j++;
if(j-i+1 == 3){
if(nums[j-1] - nums[j-2] == nums[j] - nums[j-1]){
d = nums[j-1] - nums[j-2];
j++;
}
else{
i++;
j++;
}
}
else if(j-i+1 > 3){
if(nums[j]-nums[j-1]==d)j++;
else{
int c = j - i +1 -3;
ans += ((c*(c+1))/2);
i = j-1;
}
}
}
if(j-i+1 > 3) {
int c = j - i +1 -3;
ans += ((c*(c+1))/2);
}
return ans;
}
};