22
33import java .util .Arrays ;
44import java .util .HashMap ;
5+ import java .util .HashSet ;
56import java .util .Map ;
7+ import java .util .Set ;
68
79public class LongestConsecutiveSequence {
810
911 // O(n^2) time, O(n) space
1012 public static int lengthOfLongestConsecutiveSequence (int [] arr , int N ) {
1113
14+ if (N == 0 )
15+ return 0 ;
16+
1217 Map <Integer , Integer > map = new HashMap <>();
1318 for (int n : arr ) {
1419 if (!map .containsKey (n )) {
@@ -37,16 +42,24 @@ public static int lengthOfLongestConsecutiveSequence(int[] arr, int N) {
3742 // approach 2 using Sorting
3843 // O(NlogN) time, O(N) space
3944 public static int lengthOfLongestConsecutiveSequence2 (int [] arr , int N ) {
45+ if (N == 0 )
46+ return 0 ;
47+
4048 int max = 1 ;
4149 int [] arrclone = arr .clone (); // making copy of original array
4250
4351 Arrays .sort (arrclone ); // sorting array
4452
4553 int ans = 1 ;
4654 for (int i = 1 ; i < arrclone .length ; i ++) {
55+
4756 if (arrclone [i - 1 ] == arrclone [i ] - 1 ) {
4857 ans ++;
4958 max = Math .max (ans , max );
59+
60+ } else if (arrclone [i ] == arrclone [i - 1 ]) { // for duplicate elements
61+ continue ;
62+
5063 } else {
5164 ans = 1 ;
5265 }
@@ -55,17 +68,59 @@ public static int lengthOfLongestConsecutiveSequence2(int[] arr, int N) {
5568 return max ;
5669 }
5770
71+ // approach 3, most optimal - using HashSet
72+ // O(N) time | O(N) Space
73+ public static int lengthOfLongestConsecutiveSequence3 (int [] arr , int N ) {
74+ if (N == 0 ) {
75+ return 0 ;
76+ }
77+ Set <Integer > myset = new HashSet <Integer >();
78+ for (int num : arr ) {
79+ myset .add (num );
80+ }
81+
82+ int maxlen = 1 ;
83+ for (int n : arr ) {
84+ int currmax = 1 ;
85+
86+ if (myset .contains (n - 1 )) // skip already searched sequence
87+ continue ;
88+ else {
89+ while (myset .contains (n + 1 )) {
90+ currmax ++;
91+ n ++;
92+ }
93+ }
94+ maxlen = Math .max (currmax , maxlen );
95+ }
96+
97+ return maxlen ;
98+ }
99+
58100 public static void main (String [] args ) {
59101
60- // int[] arr = { 100, 4, 200, 1, 3, 2 };
61- // int[] arr = { 1, 5, 9, 13, 6, 12, 7, 11, 14, 15 };
62- int [] arr = { 9 , 5 , 4 , 9 , 10 , 10 , 6 };
63- int [] arr2 = { 0 , 3 , 7 , 2 , 5 , 8 , 4 , 6 , 0 , 1 };
102+ int [] arr = { 100 , 4 , 200 , 1 , 3 , 2 };
103+ int [] arr2 = { 1 , 5 , 9 , 13 , 6 , 12 , 7 , 11 , 14 , 15 };
104+ int [] arr3 = { 9 , 5 , 4 , 9 , 10 , 10 , 6 };
105+ int [] arr4 = { 0 , 3 , 7 , 2 , 5 , 8 , 4 , 6 , 0 , 1 };
106+
107+ System .out .println ("brute force solution..." );
64108 System .out .println (lengthOfLongestConsecutiveSequence (arr , 7 ));
65109 System .out .println (lengthOfLongestConsecutiveSequence (arr2 , 9 ));
110+ System .out .println (lengthOfLongestConsecutiveSequence (arr3 , 7 ));
111+ System .out .println (lengthOfLongestConsecutiveSequence (arr4 , 9 ));
66112
113+ System .out .println ("\n test using sorting technique..." );
67114 System .out .println (lengthOfLongestConsecutiveSequence2 (arr , 7 ));
68115 System .out .println (lengthOfLongestConsecutiveSequence2 (arr2 , 9 ));
116+ System .out .println (lengthOfLongestConsecutiveSequence2 (arr3 , 7 ));
117+ System .out .println (lengthOfLongestConsecutiveSequence2 (arr4 , 9 ));
118+
119+ System .out .println ("\n test using hashset..." );
120+ System .out .println (lengthOfLongestConsecutiveSequence3 (arr , 7 ));
121+ System .out .println (lengthOfLongestConsecutiveSequence3 (arr2 , 9 ));
122+ System .out .println (lengthOfLongestConsecutiveSequence3 (arr3 , 7 ));
123+ System .out .println (lengthOfLongestConsecutiveSequence3 (arr4 , 9 ));
69124
70125 }
71126}
0 commit comments