-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArithmeticSlicesII-Subsequence.cpp
More file actions
46 lines (37 loc) · 962 Bytes
/
Copy pathArithmeticSlicesII-Subsequence.cpp
File metadata and controls
46 lines (37 loc) · 962 Bytes
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
/*
* Time Limit Exceeded
*/
#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
int numberOfArithmeticSlices(vector<int>& A) {
int result = 0;
for(int i = 0; i < A.size(); i++)
numberOfArithmeticSlices(A, i, 0, 1, result);
return result;
}
void numberOfArithmeticSlices(vector<int>& A, int pos, long long diff, int count, int& result) {
for(int i = pos + 1; i < A.size(); i++) {
if(count > 1 && (long long)A[i] - A[pos] == diff) {
result++;
numberOfArithmeticSlices(A, i, diff, count + 1, result);
}
else if(count <= 1) numberOfArithmeticSlices(A, i, (long long)A[i] - A[pos], count + 1, result);
}
}
};
int main() {
int n;
cin>>n;
vector<int> nums;
for(int i = 0; i < n; i++) {
int num;
cin>>num;
nums.push_back(num);
}
Solution *solution = new Solution();
cout<<solution->numberOfArithmeticSlices(nums);
return 0;
}