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)
Similar Reads
Maximum number of consecutive 1's in binary representation of all the array elements
Given an array arr[] of N elements, the task is to find the maximum number of consecutive 1's in the binary representation of an element among all the elements of the given array. Examples: Input: arr[] = {1, 2, 3, 4} Output: 2 Binary(1) = 01 Binary(2) = 10 Binary(3) = 11 Binary(4) = 100 Input: arr[
6 min read
Maximum count of equal numbers in an array after performing given operations
Given an array of integers. The task is to find the maximum count of equal numbers in an array after applying the given operation any number of times. In an operation: Choose two elements of the array a[i], a[j] (such that i is not equals to j) and, Increase number a[i] by 1 and decrease number a[j]
5 min read
Maximum XOR of Two Numbers in an Array
Given an array arr[] of non-negative integers. The task is to find the maximum possible XOR of any two elements of the array. Example: Input: arr[] = [26, 100, 25, 13, 4, 14]Output: 126 Explanation: XOR of 26 ^ 100 = 126, which is the maximum possible XOR. Input: arr[] = [1, 2, 3, 4, 5, 6, 7]Output:
15 min read
Maximum even numbers present in any subarray of size K
Given an array arr[] of size N and an integer K, the task is to find the maximum number of even numbers present in any subarray of size K. Examples: Input: arr[] = {2, 3, 5, 4, 7, 6}, K = 3 Output: 2 Explanation: Subarrays of size K(=3) with maximum count of even numbers are { arr[3], arr[4], arr[5]
12 min read
Maximise the size of consecutive element subsets in an array
Given an integer array and an integer k. The array elements denote positions of points on a 1-D number line, find the maximum size of the subset of points that can have consecutive values of points which can be formed by placing another k points on the number line. Note that all coordinates should b
12 min read
Find a number that divides maximum array elements
Given an array A[] of N non-negative integers. Find an Integer greater than 1, such that maximum array elements are divisible by it. In case of same answer print the smaller one. Examples: Input : A[] = { 2, 4, 5, 10, 8, 15, 16 }; Output : 2 Explanation: 2 divides [ 2, 4, 10, 8, 16] no other element
13 min read
Find all numbers that divide maximum array elements
Given an array of N numbers, the task is to print all the numbers greater than 1 which divides the maximum of array elements. Examples: Input: a[] = {6, 6, 12, 18, 13} Output: 2 3 6 All the numbers divide the maximum of array elements i.e., 4 Input: a[] = {12, 15, 27, 20, 40} Output: 2 3 4 5 Approac
7 min read
Maximize the equal numbers in the Array
Given an array A[] consisting of n elements and integer K. The task is to find the maximum length of the subsequence of array A[], such that all numbers in that subsequence are equal after applying the given operation. For each (1 ⤠i ⤠n), perform the following operation exactly one time: Replace A
11 min read
Maximize array elements upto given number
Given an array of integers, a number and a maximum value, task is to compute the maximum value that can be obtained from the array elements. Every value on the array traversing from the beginning can be either added to or subtracted from the result obtained from previous index such that at any point
15+ min read
Maximum length of a sub-array with ugly numbers
Given an array arr[] of N elements (0 ? arr[i] ? 1000). The task is to find the maximum length of the sub-array that contains only ugly numbers. Ugly numbers are numbers whose only prime factors are 2, 3, or 5. The sequence 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, ..... shows the first few ugly numbers.
8 min read