Skip to content

Commit 39fdc72

Browse files
committed
Commited
1 parent 5b5096e commit 39fdc72

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution {
2+
public:
3+
int lenLongestFibSubseq(vector<int>& arr) {
4+
5+
unordered_set<int>s;
6+
int n = arr.size();
7+
8+
for(int i=0;i<n;i++){
9+
s.insert(arr[i]);
10+
}
11+
12+
int ans = 0;
13+
14+
for(int i=0;i<n;i++){
15+
for(int j=i+1;j<n;j++){
16+
int first = arr[i];
17+
int second = arr[j];
18+
int next = first + second;
19+
20+
int c = 2;
21+
22+
while(s.find(next)!=s.end()){
23+
int temp = next;
24+
next = next+second;
25+
first = second;
26+
second = temp;
27+
c++;
28+
}
29+
30+
ans = max(ans,c);
31+
}
32+
}
33+
34+
if(ans == 2)
35+
return 0;
36+
37+
return ans;
38+
39+
}
40+
};

0 commit comments

Comments
 (0)