Maximum consecutive numbers present in an array
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 |
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)
Recommended Posts:
- Sum of all perfect numbers present in an array
- Sum of all palindrome numbers present in an Array
- Sum of all mersenne numbers present in an array
- Count the number of sub-arrays such that the average of elements present in the sub-array is greater than that not present in the sub-array
- Maximum consecutive one’s (or zeros) in a binary array
- Maximum consecutive one’s (or zeros) in a binary circular array
- Maximum number of consecutive 1's in binary representation of all the array elements
- Find missing element in a sorted array of consecutive numbers
- Check if array elements are consecutive in O(n) time and O(1) space (Handles Both Positive and negative numbers)
- Minimum number greater than the maximum of array which cannot be formed using the numbers in the array
- Maximum XOR of Two Numbers in an Array
- Maximum value of division of two numbers in an Array
- Maximum no. of contiguous Prime Numbers in an array
- Minimum and Maximum prime numbers in an array
- Maximum length of a sub-array with ugly numbers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

