-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIs it Fibonacci(O(N*N)).cpp
64 lines (48 loc) · 1.18 KB
/
Is it Fibonacci(O(N*N)).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
61
62
63
64
//{ Driver Code Starts
// Initial Template for C++
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
// User function Template for C++
class Solution {
public:
long long solve(int N, int K, vector<long long> GeekNum) {
// code here
if(K==1) return GeekNum[0];
for(int i=0;i<N;i++){
if(i>K-1){
long long sum=0;
int curr=i-1;
int j=0;
while(curr>=0 && j<K){
sum+=GeekNum[curr];
curr--;
j++;
}
GeekNum.push_back(sum);
}
}
// for(auto i : GeekNum){
// cout<<i<<" ";
// }
// cout<<endl;
return GeekNum[N-1];
}
};
// 7 3
// 96 54 17
//{ Driver Code Starts.
int main() {
int T;
cin >> T;
while (T--) {
int N, K;
cin >> N >> K;
vector<long long> GeekNum(K);
for (int i = 0; i < K; i++) cin >> GeekNum[i];
Solution ob;
cout << ob.solve(N, K, GeekNum) << "\n";
}
return 0;
}
// } Driver Code Ends