Given an array of integers, find length of the longest subarray which contains numbers that can be arranged in a continuous sequence.
In the previous post, we have discussed a solution that assumes that elements in given array are distinct. Here we discuss a solution that works even if the input array has duplicates.
Examples:
Input: arr[] = {10, 12, 11};
Output: Length of the longest contiguous subarray is 3
Input: arr[] = {10, 12, 12, 10, 10, 11, 10};
Output: Length of the longest contiguous subarray is 2
The idea is similar to the previous post. In the previous post, we checked whether the maximum value minus the minimum value is equal to the ending index minus starting index or not. Since duplicate elements are allowed, we also need to check if the subarray contains duplicate elements or not. For example, the array {12, 14, 12} follows the first property, but its numbers are not contiguous elements.
To check duplicate elements in a subarray, we create a hash set for every subarray and if we find an element already in the hash, we don’t consider the current subarray.
Following is the implementation of the above idea.
C++
#include<bits/stdc++.h>
using namespace std;
int findLength( int arr[], int n)
{
int max_len = 1;
for ( int i=0; i<n-1; i++)
{
unordered_set< int > myset;
myset.insert(arr[i]);
int mn = arr[i], mx = arr[i];
for ( int j=i+1; j<n; j++)
{
if (myset.find(arr[j]) != myset.end())
break ;
myset.insert(arr[j]);
mn = min(mn, arr[j]);
mx = max(mx, arr[j]);
if (mx - mn == j - i)
max_len = max(max_len, mx - mn + 1);
}
}
return max_len;
}
int main ()
{
int arr[] = {10, 12, 12, 10, 10, 11, 10};
int n = sizeof (arr) / sizeof (arr[0]);
cout << "Length of the longest contiguous"
<< " subarray is " << findLength(arr, n);
}
|
Java
import java.util.*;
class Main
{
static int findLength( int arr[])
{
int n = arr.length;
int max_len = 1 ;
for ( int i= 0 ; i<n- 1 ; i++)
{
HashSet<Integer> set = new HashSet<>();
set.add(arr[i]);
int mn = arr[i], mx = arr[i];
for ( int j=i+ 1 ; j<n; j++)
{
if (set.contains(arr[j]))
break ;
set.add(arr[j]);
mn = Math.min(mn, arr[j]);
mx = Math.max(mx, arr[j]);
if (mx-mn == j-i)
max_len = Math.max(max_len, mx-mn+ 1 );
}
}
return max_len;
}
public static void main (String[] args)
{
int arr[] = { 10 , 12 , 12 , 10 , 10 , 11 , 10 };
System.out.println( "Length of the longest contiguous subarray is " +
findLength(arr));
}
}
|
Python3
def findLenght(arr, n):
max_len = 1
for i in range ( 0 ,n - 1 ):
myset = set ()
myset.add(arr[i])
mn = arr[i]
mx = arr[i]
for j in range (i + 1 ,n):
if arr[j] in myset:
break
myset.add(arr[j])
mn = min (mn, arr[j])
mx = max (mx, arr[j])
if mx - mn = = j - i:
max_len = max (max_len, mx - mn + 1 )
return max_len
arr = [ 10 , 12 , 12 , 10 , 10 , 11 , 10 ]
n = len (arr)
print ( "Length of the longest contiguous subarray is" ,
findLenght(arr,n))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int findLength( int []arr)
{
int n = arr.Length;
int max_len = 1;
for ( int i = 0; i < n-1; i++)
{
HashSet< int > set = new HashSet< int >();
set .Add(arr[i]);
int mn = arr[i], mx = arr[i];
for ( int j = i+1; j < n; j++)
{
if ( set .Contains(arr[j]))
break ;
set .Add(arr[j]);
mn = Math.Min(mn, arr[j]);
mx = Math.Max(mx, arr[j]);
if (mx-mn == j-i)
max_len = Math.Max(max_len,
mx - mn + 1);
}
}
return max_len;
}
public static void Main()
{
int []arr = {10, 12, 12, 10, 10, 11, 10};
Console.WriteLine( "Length of the longest"
+ " contiguous subarray is " +
findLength(arr));
}
}
|
Javascript
<script>
function findLength(arr) {
var n = arr.length;
var max_len = 1;
for (i = 0; i < n - 1; i++) {
var set = new Set();
set.add(arr[i]);
var mn = arr[i], mx = arr[i];
for (j = i + 1; j < n; j++) {
if (set.has(arr[j]))
break ;
set.add(arr[j]);
mn = Math.min(mn, arr[j]);
mx = Math.max(mx, arr[j]);
if (mx - mn == j - i)
max_len = Math.max(max_len, mx - mn + 1);
}
}
return max_len;
}
var arr = [ 10, 12, 12, 10, 10, 11, 10 ];
document.write( "Length of the longest contiguous subarray is "
+ findLength(arr));
</script>
|
OutputLength of the longest contiguous subarray is 2
Time complexity of the above solution is O(n2) under the assumption that hash set operations like add() and contains() work in O(1) time.