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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output:

3

Time complexity : O(n)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : Rajput-Ji, 29AjayKumar



Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.