Find the length of maximum number of consecutive numbers jumbled up in an array.
Examples:
Input : arr[] = {1, 94, 93, 1000, 5, 92, 78}; Output : 3 The largest set of consecutive elements is 92, 93, 94 Input : arr[] = {1, 5, 92, 4, 78, 6, 7}; Output : 4 The largest set of consecutive elements is 4, 5, 6, 7
The idea is to use hashing. We traverse through the array and for every element, we check if it is the starting element of its sequence. If yes then by incrementing its value we search the set and increment the length. By repeating this for all elements, we can find the lengths of all consecutive sets in array. Finally we return length of the largest set.
C++
// CPP program to find largest consecutive numbers // present in arr[]. #include <bits/stdc++.h> using namespace std; int findLongestConseqSubseq( int arr[], int n) { /* We insert all the array elements into unordered set. */ unordered_set< int > S; for ( int i = 0; i < n; i++) S.insert(arr[i]); // check each possible sequence from the start // then update optimal length int ans = 0; for ( int i = 0; i < n; i++) { // if current element is the starting // element of a sequence if (S.find(arr[i] - 1) == S.end()) { // Then check for next elements in the // sequence int j = arr[i]; // increment the value of array element // and repeat search in the set while (S.find(j) != S.end()) j++; // Update optimal length if this length // is more. To get the length as it is // incremented one by one ans = max(ans, j - arr[i]); } } return ans; } // Driver code int main() { int arr[] = { 1, 94, 93, 1000, 5, 92, 78 }; int n = sizeof (arr) / sizeof ( int ); cout << findLongestConseqSubseq(arr, n) << endl; return 0; } |
Java
// Java program to find largest consecutive // numbers present in arr[]. import java.util.*; class GFG { static int findLongestConseqSubseq( int arr[], int n) { /* We insert all the array elements into unordered set. */ HashSet<Integer> S = new HashSet<Integer>(); for ( int i = 0 ; i < n; i++) S.add(arr[i]); // check each possible sequence from the start // then update optimal length int ans = 0 ; for ( int i = 0 ; i < n; i++) { // if current element is the starting // element of a sequence if (S.contains(arr[i])) { // Then check for next elements in the // sequence int j = arr[i]; // increment the value of array element // and repeat search in the set while (S.contains(j)) j++; // Update optimal length if this length // is more. To get the length as it is // incremented one by one ans = Math.max(ans, j - arr[i]); } } return ans; } // Driver code public static void main(String[] args) { int arr[] = { 1 , 94 , 93 , 1000 , 5 , 92 , 78 }; int n = arr.length; System.out.println(findLongestConseqSubseq(arr, n)); } } // This code contributed by Rajput-Ji |
Python3
# Python3 program to find largest consecutive # numbers present in arr. def findLongestConseqSubseq(arr, n): '''We insert all the array elements into unordered set.''' S = set (); for i in range (n): S.add(arr[i]); # check each possible sequence from the start # then update optimal length ans = 0 ; for i in range (n): # if current element is the starting # element of a sequence if S.__contains__(arr[i]): # Then check for next elements in the # sequence j = arr[i]; # increment the value of array element # and repeat search in the set while (S.__contains__(j)): j + = 1 ; # Update optimal length if this length # is more. To get the length as it is # incremented one by one ans = max (ans, j - arr[i]); return ans; # Driver code if __name__ = = '__main__' : arr = [ 1 , 94 , 93 , 1000 , 5 , 92 , 78 ]; n = len (arr); print (findLongestConseqSubseq(arr, n)); # This code is contributed by 29AjayKumar |
C#
// C# program to find largest consecutive // numbers present in arr[]. using System; using System.Collections.Generic; public class GFG { static int findLongestConseqSubseq( int []arr, int n) { /* We insert all the array elements into unordered set. */ HashSet< int > S = new HashSet< int >(); for ( int i = 0; i < n; i++) S.Add(arr[i]); // check each possible sequence from the start // then update optimal length int ans = 0; for ( int i = 0; i < n; i++) { // if current element is the starting // element of a sequence if (S.Contains(arr[i])) { // Then check for next elements in the // sequence int j = arr[i]; // increment the value of array element // and repeat search in the set while (S.Contains(j)) j++; // Update optimal length if this length // is more. To get the length as it is // incremented one by one ans = Math.Max(ans, j - arr[i]); } } return ans; } // Driver code public static void Main(String[] args) { int []arr = {1, 94, 93, 1000, 5, 92, 78}; int n = arr.Length; Console.WriteLine(findLongestConseqSubseq(arr, n)); } } // This code has been contributed by 29AjayKumar |
Output:
3
Time complexity : O(n)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.