-
Notifications
You must be signed in to change notification settings - Fork 1
/
longestCOnsecutive.java
66 lines (50 loc) · 1.65 KB
/
longestCOnsecutive.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package aryanhere.Striver;
import java.util.Arrays;
import java.util.HashSet;
public class longestCOnsecutive {
public static int longestConsecutive1(int[] nums) {
HashSet<Integer> hashSet = new HashSet<>();
for (int num : nums) {
hashSet.add(num);
}
int longestStreak = 0;
for (int num : nums) {
if (!hashSet.contains(num - 1)) {
int currentNum = num;
int currentStreak = 1;
while (!hashSet.contains(currentNum + 1)) {
currentNum += 1;
currentStreak += 1;
}
longestStreak = Math.max(longestStreak, currentStreak);
}
}
return longestStreak;
}
public static int longestConsecutiv(int[]nums){
if(nums.length == 0 || nums == null){
return 0 ;
}
Arrays.sort(nums);
int ans = 1;
int prev = nums[0];
int curr = 1;
// smjh mein to aagya kya ho rha par mene question ni pdha tha dhng se to confusion ho rha hai.. hahahah..!
for(int i = 0 ; i < nums.length; i++){
if(nums[i] == prev + 1){
curr++;
}else if(nums[i] != prev){
curr = 1;
}
prev = nums[i];
curr = Math.max(ans, curr);
}
return ans;
}
public static void main(String[]args){
int arr[] = {100, 200 , 1, 2, 3, 4};
int ans = longestConsecutive1(arr);
System.out.println("The longest consecutive is " + ans);
}
//pendingggg
}