Created
April 11, 2022 13:34
-
-
Save akshay-ravindran-96/6b70c9ee086f62d8759dd4195cb42c8a to your computer and use it in GitHub Desktop.
longestConsecutive_graph.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public int longestConsecutive(int[] nums) { | |
if (nums.length == 0) return 0; | |
Set<Integer> set = new HashSet<>(); | |
for (int i = 0; i < nums.length; i++) { | |
set.add(nums[i]); | |
} | |
int res = 1; | |
for (int i = 0; i < nums.length; i++) { | |
if(set.contains(nums[i]-1)) continue; | |
int counter=0; | |
while (set.contains(nums[i] + counter)){ | |
set.remove(nums[i]+counter); | |
counter++; | |
} | |
res = Math.max(res,counter); | |
} | |
return res; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment