Skip to content

Commit 249a47c

Browse files
committed
Longest consecutive subsequence
1 parent a0197a2 commit 249a47c

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

Diff for: .vscode/settings.json

+8-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99
"valarray": "cpp",
1010
"chrono": "cpp",
1111
"random": "cpp",
12-
"limits": "cpp"
12+
"limits": "cpp",
13+
"deque": "cpp",
14+
"forward_list": "cpp",
15+
"list": "cpp",
16+
"string": "cpp",
17+
"unordered_map": "cpp",
18+
"unordered_set": "cpp",
19+
"vector": "cpp"
1320
}
1421
}

Diff for: DSA Crack Sheet/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
- [Subarray with 0 sum](https://practice.geeksforgeeks.org/problems/subarray-with-0-sum-1587115621/1# "view question") - [Cpp Solution](./solutions/Subarray%20with%200%20sum.cpp)
2525
- [Factorials of large numbers](https://practice.geeksforgeeks.org/problems/factorials-of-large-numbers/0# "view question") - [Cpp Solution](./solutions/Factorials%20of%20large%20numbers.cpp)
2626
- [Maximum Product Subarray](https://practice.geeksforgeeks.org/problems/maximum-product-subarray3604/1 "view question") - [Cpp Solution](./solutions/Maximum%20Product%20Subarray.cpp)
27+
- [Longest consecutive subsequence](https://practice.geeksforgeeks.org/problems/longest-consecutive-subsequence2449/1# "view question") - [Cpp Solution](./solutions/Longest%20consecutive%20subsequence.cpp)
2728
- []( "view question") - [Cpp Solution](./solutions/.cpp)
2829

2930
<!-- - []( "view question") - [Cpp Solution](./solutions/) -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
Longest consecutive subsequence
3+
===============================
4+
5+
Given an array of positive integers. Find the length of the longest sub-sequence such that elements in the subsequence are consecutive integers, the consecutive numbers can be in any order.
6+
7+
Example 1:
8+
Input:
9+
N = 7
10+
a[] = {2,6,1,9,4,5,3}
11+
Output:
12+
6
13+
Explanation:
14+
The consecutive numbers here
15+
are 1, 2, 3, 4, 5, 6. These 6
16+
numbers form the longest consecutive
17+
subsquence.
18+
19+
Example 2:
20+
Input:
21+
N = 7
22+
a[] = {1,9,3,10,4,20,2}
23+
Output:
24+
4
25+
Explanation:
26+
1, 2, 3, 4 is the longest
27+
consecutive subsequence.
28+
Your Task:
29+
You don't need to read input or print anything. Your task is to complete the function findLongestConseqSubseq() which takes the array arr[] and the size of the array as inputs and returns the length of the longest subsequence of consecutive integers.
30+
31+
Expected Time Complexity: O(N).
32+
Expected Auxiliary Space: O(N).
33+
34+
Constraints:
35+
1 <= N <= 105
36+
0 <= a[i] <= 105
37+
*/
38+
39+
// arr[] : the input array
40+
// N : size of the array arr[]
41+
42+
// return the length of the longest subsequene of consecutive integers
43+
int findLongestConseqSubseq(int arr[], int N)
44+
{
45+
int ans = 1;
46+
unordered_set<int> s;
47+
for (int i = 0; i < N; ++i)
48+
s.insert(arr[i]);
49+
for (int i = 0; i < N; ++i)
50+
{
51+
if (s.find(arr[i] - 1) == s.end())
52+
{
53+
int elem = arr[i];
54+
while (s.find(elem) != s.end())
55+
elem++;
56+
ans = max(ans, elem - arr[i]);
57+
}
58+
}
59+
60+
return ans;
61+
}

0 commit comments

Comments
 (0)