-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProblem-2.java
49 lines (37 loc) · 888 Bytes
/
Problem-2.java
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
/**
* @author AkashGoyal
* @date 31/05/2021
*/
/**
--------------------- Problem----------->> Longest consecutive subsequence
Problem Link :- https://practice.geeksforgeeks.org/problems/longest-consecutive-subsequence2449/1
Reference:- O(N)+O(N)+O(N)--> https://www.youtube.com/watch?v=qgizvmgeyUM
*/
static int findLongestConseqSubseq(int arr[], int N)
{
// add your code here
int max_subs=0;
HashSet<Integer>hs=new HashSet<>();
for(int i=0;i<N;i++)
{
hs.add(arr[i]);
}
for(int i=0;i<N;i++)
{
int local_count=0;
if(hs.contains(arr[i]-1))
{
continue;
}
int point=arr[i];
while(hs.contains(point)){
local_count++;
point++;
}
if(max_subs<local_count)
{
max_subs=local_count;
}
}
return max_subs;
}