forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArithmetic Slices.cpp
32 lines (29 loc) · 927 Bytes
/
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
// 😉😉😉😉Please upvote if it helps 😉😉😉😉
class Solution {
public:
int numberOfArithmeticSlices(vector<int>& nums) {
// if nums size is less than 3 return false
if(nums.size() < 3)
return 0;
int cnt = 0, diff;
for(int i = 0; i<nums.size()-2; ++i)
{
// storing diff of first 2 elements
diff = nums[i+1] - nums[i];
// checking for consecutive elements with same difference.
for(int j = i+2; j<nums.size(); ++j)
{
// if we find the same diff of next 2 elements
// this means we find consecutive elements
// increase the Count
if(nums[j] - nums[j-1] == diff)
++cnt;
else
// break as we need to cnt for consecutive diff elements
break;
}
}
// return cnt
return cnt;
}
};