Given an array of distinct integers, find length of the longest subarray which contains numbers that can be arranged in a continuous sequence.
Examples:
Input: arr[] = {10, 12, 11};
Output: Length of the longest contiguous subarray is 3
Input: arr[] = {14, 12, 11, 20};
Output: Length of the longest contiguous subarray is 2
Input: arr[] = {1, 56, 58, 57, 90, 92, 94, 93, 91, 45};
Output: Length of the longest contiguous subarray is 5
We strongly recommend to minimize the browser and try this yourself first.
The important thing to note in question is, it is given that all elements are distinct. If all elements are distinct, then a subarray has contiguous elements if and only if the difference between maximum and minimum elements in subarray is equal to the difference between last and first indexes of subarray. So the idea is to keep track of minimum and maximum element in every subarray.
The following is the implementation of above idea.
C++
#include<iostream>
using namespace std;
int min( int x, int y) { return (x < y)? x : y; }
int max( int x, int y) { return (x > y)? x : y; }
int findLength( int arr[], int n)
{
int max_len = 1;
for ( int i=0; i<n-1; i++)
{
int mn = arr[i], mx = arr[i];
for ( int j=i+1; j<n; 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[] = {1, 56, 58, 57, 90, 92, 94, 93, 91, 45};
int n = sizeof (arr)/ sizeof (arr[0]);
cout << "Length of the longest contiguous subarray is "
<< findLength(arr, n);
return 0;
}
|
Java
class LargestSubArray2
{
int min( int x, int y)
{
return (x < y) ? x : y;
}
int max( int x, int y)
{
return (x > y) ? x : y;
}
int findLength( int arr[], int n)
{
int max_len = 1 ;
for ( int i = 0 ; i < n - 1 ; i++)
{
int mn = arr[i], mx = arr[i];
for ( int j = i + 1 ; j < n; 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;
}
public static void main(String[] args)
{
LargestSubArray2 large = new LargestSubArray2();
int arr[] = { 1 , 56 , 58 , 57 , 90 , 92 , 94 , 93 , 91 , 45 };
int n = arr.length;
System.out.println( "Length of the longest contiguous subarray is "
+ large.findLength(arr, n));
}
}
|
Python3
def min (x, y):
return x if (x < y) else y
def max (x, y):
return x if (x > y) else y
def findLength(arr, n):
max_len = 1
for i in range (n - 1 ):
mn = arr[i]
mx = arr[i]
for j in range (i + 1 , n):
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 = [ 1 , 56 , 58 , 57 , 90 , 92 , 94 , 93 , 91 , 45 ]
n = len (arr)
print ( "Length of the longest contiguous subarray is " ,
findLength(arr, n))
|
C#
using System;
class GFG {
static int findLength( int []arr, int n)
{
int max_len = 1;
for ( int i = 0; i < n - 1; i++)
{
int mn = arr[i], mx = arr[i];
for ( int j = i + 1; j < n; 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 = {1, 56, 58, 57, 90, 92,
94, 93, 91, 45};
int n = arr.Length;
Console.WriteLine( "Length of the longest"
+ " contiguous subarray is "
+ findLength(arr, n));
}
}
|
PHP
<?php
function mins( $x , $y )
{
if ( $x < $y )
return $x ;
else
return $y ;
}
function maxi( $a , $b )
{
if ( $a > $b )
return $a ;
else
return $b ;
}
function findLength(& $arr , $n )
{
$max_len = 1;
for ( $i = 0; $i < $n - 1; $i ++)
{
$mn = $arr [ $i ];
$mx = $arr [ $i ];
for ( $j = $i + 1; $j < $n ; $j ++)
{
$mn = mins( $mn , $arr [ $j ]);
$mx = maxi( $mx , $arr [ $j ]);
if (( $mx - $mn ) == $j - $i )
$max_len = maxi( $max_len ,
$mx - $mn + 1);
}
}
return $max_len ;
}
$arr = array (1, 56, 58, 57, 90,
92, 94, 93, 91, 45);
$n = sizeof( $arr );
echo ( "Length of the longest contiguous" .
" subarray is " );
echo (findLength( $arr , $n ));
?>
|
Javascript
<script>
function min( x, y) { return (x < y)? x : y; }
function max( x, y) { return (x > y)? x : y; }
function findLength( arr, n)
{
let max_len = 1;
for (let i=0; i<n-1; i++)
{
let mn = arr[i], mx = arr[i];
for (let j=i+1; j<n; j++)
{
mn = min(mn, arr[j]);
mx = max(mx, arr[j]);
if ((mx - mn) == j-i)
max_len = Math.max(max_len, mx-mn+1);
}
}
return max_len;
}
let arr = [1, 56, 58, 57, 90, 92, 94, 93, 91, 45];
let n = arr.length;
document.write( "Length of the longest contiguous subarray is "
+ findLength(arr, n));
</script>
|
OutputLength of the longest contiguous subarray is 5
Time Complexity of the above solution is O(n2).
Auxiliary Space: O(1) ,since no extra space is used.
We will soon be covering solution for the problem where duplicate elements are allowed in subarray.
Length of the largest subarray with contiguous elements | Set 2