-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathHashmaps:Longest consecutive Sequence
More file actions
92 lines (74 loc) · 2.48 KB
/
Hashmaps:Longest consecutive Sequence
File metadata and controls
92 lines (74 loc) · 2.48 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import java.util.HashMap;
import java.util.ArrayList;
public class Solution {
public static ArrayList<Integer> longestConsecutiveIncreasingSequence(int[] arr) {
public static ArrayList<Integer> longestConsecutiveIncreasingSequence(int[] arr) {
ArrayList<Integer> output = new ArrayList<>();
HashMap<Integer, Boolean> map = new HashMap<>();
HashMap<Integer, Integer> lenMap = new HashMap<>();
for (int i=0 ; i < arr.length ; i++)
{
map.put(arr[i],true);
}
int maxStart=-1,maxLen=0;
boolean startCheck=true;
for (int i: arr)
{
if (map.get(i))
{
int currStart=i,currLen=1;
boolean flag=true;
map.put(i,false);
int ahead=i+1;
while(flag)
{
if(map.containsKey(ahead))
{
currLen=currLen+1;
map.put(ahead,false);
ahead=ahead+1;
}
else
{
flag=false;
}
}
flag=true;
int before=i-1;
while(flag)
{
if(map.containsKey(before))
{
currLen=currLen+1;
currStart=before;
map.put(before,false);
before=before-1;
}
else
{
flag=false;
}
}
System.out.println();
if (currLen>=maxLen)
{
maxLen=currLen;
maxStart=currStart;
lenMap.put(maxStart,maxLen);
}
}
}
for (int i=0;i<arr.length;i++)
{
if (lenMap.containsKey(arr[i]) && lenMap.get(arr[i])>=maxLen)
{
maxStart=arr[i];
maxLen=lenMap.get(arr[i]);
break;
}
}
output.add(maxStart);
output.add(maxStart+maxLen-1);
return output;
}
}