Maximum consecutive numbers present in an array
Last Updated :
12 Jul, 2023
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++
#include <bits/stdc++.h>
using namespace std;
int findLongestConseqSubseq( int arr[], int n)
{
unordered_set< int > S;
for ( int i = 0; i < n; i++)
S.insert(arr[i]);
int ans = 0;
for ( int i = 0; i < n; i++) {
if (S.find(arr[i] - 1) == S.end()) {
int j = arr[i];
while (S.find(j) != S.end())
j++;
ans = max(ans, j - arr[i]);
}
}
return ans;
}
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
import java.util.*;
class GFG
{
static int findLongestConseqSubseq( int arr[], int n)
{
HashSet<Integer> S = new HashSet<Integer>();
for ( int i = 0 ; i < n; i++)
S.add(arr[i]);
int ans = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (S.contains(arr[i]))
{
int j = arr[i];
while (S.contains(j))
j++;
ans = Math.max(ans, j - arr[i]);
}
}
return ans;
}
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));
}
}
|
Python3
def findLongestConseqSubseq(arr, n):
S = set ();
for i in range (n):
S.add(arr[i]);
ans = 0 ;
for i in range (n):
if S.__contains__(arr[i]):
j = arr[i];
while (S.__contains__(j)):
j + = 1 ;
ans = max (ans, j - arr[i]);
return ans;
if __name__ = = '__main__' :
arr = [ 1 , 94 , 93 , 1000 , 5 , 92 , 78 ];
n = len (arr);
print (findLongestConseqSubseq(arr, n));
|
C#
using System;
using System.Collections.Generic; public
class GFG
{
static int findLongestConseqSubseq( int []arr, int n)
{
HashSet< int > S = new HashSet< int >();
for ( int i = 0; i < n; i++)
S.Add(arr[i]);
int ans = 0;
for ( int i = 0; i < n; i++)
{
if (S.Contains(arr[i]))
{
int j = arr[i];
while (S.Contains(j))
j++;
ans = Math.Max(ans, j - arr[i]);
}
}
return ans;
}
public static void Main(String[] args)
{
int []arr = {1, 94, 93, 1000, 5, 92, 78};
int n = arr.Length;
Console.WriteLine(findLongestConseqSubseq(arr, n));
}
}
|
Javascript
<script>
function findLongestConseqSubseq(arr, n) {
let S = new Set();
for (let i = 0; i < n; i++)
S.add(arr[i]);
let ans = 0;
for (let i = 0; i < n; i++) {
if (!S.has(arr[i] - 1)) {
let j = arr[i];
while (S.has(j))
j++;
ans = Math.max(ans, j - arr[i]);
}
}
return ans;
}
let arr = [1, 94, 93, 1000, 5, 92, 78];
let n = arr.length;
document.write(findLongestConseqSubseq(arr, n) + "<br>" );
</script>
|
Time complexity : O(n)
Space complexity: O(n)
Another approach: The idea is to sort the array. We will traverse through the array and check if the difference between the current element and the previous element is one or not. If the difference is one we will increment the count of the length of the current sequence. Otherwise, we will check if the count of the length of our current subsequence is greater than the length of our previously counted sequence. If it is, we will update our answer and then we will update the count to one to start counting the length of another sequence. By repeating this for all elements, we can find the lengths of all consecutive sequences in the array. Finally, we return the length of the largest sequence.
C++
#include <bits/stdc++.h>
using namespace std;
int findLongestConseqSubseq( int arr[], int n)
{
if (n == 0) {
return 0;
}
sort(arr, arr + n);
int ans = 1;
int count = 1;
for ( int i = 1; i < n; i++)
{
if (arr[i] != arr[i - 1])
{
if (arr[i] - arr[i - 1] == 1) {
count += 1;
}
else {
ans = max(ans, count);
count = 1;
}
}
}
return max(ans, count);
}
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
import java.io.*;
import java.util.*;
class GFG {
static int findLongestConseqSubseq( int [] arr, int n)
{
if (n == 0 ) {
return 0 ;
}
Arrays.sort(arr);
int ans = 1 ;
int count = 1 ;
for ( int i = 1 ; i < n; i++) {
if (arr[i] != arr[i - 1 ]) {
if (arr[i] - arr[i - 1 ] == 1 ) {
count += 1 ;
}
else {
ans = Math.max(ans, count);
count = 1 ;
}
}
}
return Math.max(ans, count);
}
public static void main(String[] args)
{
int [] arr = { 1 , 94 , 93 , 1000 , 5 , 92 , 78 };
int n = arr.length;
System.out.print(findLongestConseqSubseq(arr, n));
}
}
|
Python3
def findLongestConseqSubseq(arr, n):
if n = = 0 :
return 0
arr.sort()
ans = 1
count = 1
for i in range ( 1 , n):
if arr[i]! = arr[i - 1 ]:
if arr[i] - arr[i - 1 ] = = 1 :
count + = 1
else :
ans = max (ans, count)
count = 1
return max (ans,count)
if __name__ = = '__main__' :
arr = [ 1 , 94 , 93 , 1000 , 5 , 92 , 78 ]
n = len (arr)
print (findLongestConseqSubseq(arr, n))
|
C#
using System;
using System.Collections;
public class GFG {
static int findLongestConseqSubseq( int [] arr, int n)
{
if (n == 0) {
return 0;
}
Array.Sort(arr);
int ans = 1;
int count = 1;
for ( int i = 1; i < n; i++) {
if (arr[i] != arr[i - 1]) {
if (arr[i] - arr[i - 1] == 1) {
count += 1;
}
else {
ans = Math.Max(ans, count);
count = 1;
}
}
}
return Math.Max(ans, count);
}
static public void Main()
{
int [] arr = { 1, 94, 93, 1000, 5, 92, 78 };
int n = arr.Length;
Console.Write(findLongestConseqSubseq(arr, n));
}
}
|
Javascript
function findLongestConseqSubseq(arr, n) {
if (n == 0) return 0;
arr.sort((a, b) => a - b);
let ans = 1,
count = 1;
for (let i = 1; i < n; i++) {
if (arr[i] != arr[i - 1]) {
if (arr[i] - arr[i - 1] == 1) {
count++;
}
else {
ans = Math.max(ans, count);
count = 1;
}
}
}
return Math.max(ans, count);
}
let arr = [1, 94, 93, 1000, 5, 92, 78];
let n = arr.length;
console.log(findLongestConseqSubseq(arr, n));
|
Time complexity : O(nlogn)
Space complexity: O(1)
Another approach: The idea is to use set. We traverse through the array and for every element, we check if it is the starting element of its sequence( no element whose value is less than the current element by one is present in the set ). If yes then by incrementing its value we search for other valid elements that could be present in the set and increment the length of the sequence accordingly. By repeating this for all elements, we can find the lengths of all consecutive sequences in the array. Finally, we return the length of the largest sequence
C++
#include <iostream>
#include <set>
using namespace std;
int findLongestConseqSubseq( int arr[], int n)
{
set< int > S;
for ( int i = 0; i < n; i++)
S.insert(arr[i]);
int ans = 0;
for ( int i = 0; i < n; i++)
{
if (S.find(arr[i] - 1) == S.end())
{
int j = arr[i];
while (S.find(j) != S.end())
j++;
ans = max(ans, j - arr[i]);
}
}
return ans;
}
int main()
{
int arr[] = { 1, 94, 93, 1000, 5, 92, 78 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << findLongestConseqSubseq(arr, n) << endl;
return 0;
}
|
Java
import java.util.HashSet;
import java.util.Set;
public class Main {
public static int findLongestConseqSubseq( int [] arr, int n)
{
Set<Integer> S = new HashSet<>();
for ( int i = 0 ; i < n; i++)
S.add(arr[i]);
int ans = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (!S.contains(arr[i] - 1 ))
{
int j = arr[i];
while (S.contains(j))
j++;
ans = Math.max(ans, j - arr[i]);
}
}
return ans;
}
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));
}
}
|
Python3
def findLongestConseqSubseq(arr, n):
S = set (arr)
ans = 0
for e in arr:
i = e
count = 1
if i - 1 not in S:
while i + 1 in S:
i + = 1
count + = 1
ans = max (ans, count)
return ans
if __name__ = = '__main__' :
arr = [ 1 , 94 , 93 , 1000 , 5 , 92 , 78 ]
n = len (arr)
print (findLongestConseqSubseq(arr, n))
|
C#
using System;
using System.Collections.Generic;
public class MainClass {
public static int findLongestConseqSubseq( int [] arr, int n) {
HashSet< int > S = new HashSet< int >();
foreach ( int x in arr)
S.Add(x);
int ans = 0;
foreach ( int x in arr) {
if (!S.Contains(x - 1)) {
int j = x;
while (S.Contains(j)) j++;
ans = Math.Max(ans, j - x);
}
}
return ans;
}
public static void Main() {
int [] arr = { 1, 94, 93, 1000, 5, 92, 78 };
int n = arr.Length;
Console.WriteLine(findLongestConseqSubseq(arr, n));
}
}
|
Javascript
function findLongestConseqSubseq(arr, n)
{
let S = new Set(arr);
let ans = 0;
for (let i = 0; i < n; i++)
{
if (!S.has(arr[i] - 1))
{
let j = arr[i];
while (S.has(j)) j++;
ans = Math.max(ans, j - arr[i]);
}
}
return ans;
}
let arr = [1, 94, 93, 1000, 5, 92, 78];
let n = arr.length;
console.log(findLongestConseqSubseq(arr, n));
|
Time complexity: O(nlogn)
Space complexity: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...