Skip to content

Commit fcb46fe

Browse files
authored
Create longest-arithmetic-subsequence-of-given-difference.cpp
1 parent f378255 commit fcb46fe

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Time: O(n)
2+
// Space: O(n)
3+
4+
class Solution {
5+
public:
6+
int longestSubsequence(vector<int>& arr, int difference) {
7+
int result = 1;
8+
unordered_map<int,int> lookup;
9+
for (int i = 0 ; i < arr.size() ; ++i){
10+
lookup[arr[i]] = lookup[arr[i] - difference] + 1;
11+
result = max(result, lookup[arr[i]]);
12+
}
13+
return result;
14+
}
15+
};

0 commit comments

Comments
 (0)