Length of the largest subarray with contiguous elements | Set 1
Last Updated :
20 Feb, 2023
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;
// Utility functions to find minimum and maximum of
// two elements
int min(int x, int y) { return (x < y)? x : y; }
int max(int x, int y) { return (x > y)? x : y; }
// Returns length of the longest contiguous subarray
int findLength(int arr[], int n)
{
int max_len = 1; // Initialize result
for (int i=0; i<n-1; i++)
{
// Initialize min and max for all subarrays starting with i
int mn = arr[i], mx = arr[i];
// Consider all subarrays starting with i and ending with j
for (int j=i+1; j<n; j++)
{
// Update min and max in this subarray if needed
mn = min(mn, arr[j]);
mx = max(mx, arr[j]);
// If current subarray has all contiguous elements
if ((mx - mn) == j-i)
max_len = max(max_len, mx-mn+1);
}
}
return max_len; // Return result
}
// Driver program to test above function
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
{
// Utility functions to find minimum and maximum of
// two elements
int min(int x, int y)
{
return (x < y) ? x : y;
}
int max(int x, int y)
{
return (x > y) ? x : y;
}
// Returns length of the longest contiguous subarray
int findLength(int arr[], int n)
{
int max_len = 1; // Initialize result
for (int i = 0; i < n - 1; i++)
{
// Initialize min and max for all subarrays starting with i
int mn = arr[i], mx = arr[i];
// Consider all subarrays starting with i and ending with j
for (int j = i + 1; j < n; j++)
{
// Update min and max in this subarray if needed
mn = min(mn, arr[j]);
mx = max(mx, arr[j]);
// If current subarray has all contiguous elements
if ((mx - mn) == j - i)
max_len = max(max_len, mx - mn + 1);
}
}
return max_len; // Return result
}
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));
}
}
// This code has been contributed by Mayank Jaiswal
Python3
# Python3 program to find length
# of the longest subarray
# Utility functions to find minimum
# and maximum of two elements
def min(x, y):
return x if(x < y) else y
def max(x, y):
return x if(x > y) else y
# Returns length of the longest
# contiguous subarray
def findLength(arr, n):
# Initialize result
max_len = 1
for i in range(n - 1):
# Initialize min and max for
# all subarrays starting with i
mn = arr[i]
mx = arr[i]
# Consider all subarrays starting
# with i and ending with j
for j in range(i + 1, n):
# Update min and max in
# this subarray if needed
mn = min(mn, arr[j])
mx = max(mx, arr[j])
# If current subarray has
# all contiguous elements
if ((mx - mn) == j - i):
max_len = max(max_len, mx - mn + 1)
return max_len
# Driver Code
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))
# This code is contributed by Anant Agarwal.
C#
using System;
class GFG {
// Returns length of the longest
// contiguous subarray
static int findLength(int []arr, int n)
{
int max_len = 1; // Initialize result
for (int i = 0; i < n - 1; i++)
{
// Initialize min and max for all
// subarrays starting with i
int mn = arr[i], mx = arr[i];
// Consider all subarrays starting
// with i and ending with j
for (int j = i + 1; j < n; j++)
{
// Update min and max in this
// subarray if needed
mn = Math.Min(mn, arr[j]);
mx = Math.Max(mx, arr[j]);
// If current subarray has all
// contiguous elements
if ((mx - mn) == j - i)
max_len = Math.Max(max_len,
mx - mn + 1);
}
}
return max_len; // Return result
}
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));
}
}
// This code is contributed by Sam007.
PHP
<?php
// Utility functions to find minimum
// and maximum of two elements
function mins($x, $y)
{
if($x < $y)
return $x;
else
return $y;
}
function maxi($a, $b)
{
if($a > $b)
return $a;
else
return $b;
}
// Returns length of the longest
// contiguous subarray
function findLength(&$arr, $n)
{
$max_len = 1; // Initialize result
for ($i = 0; $i < $n - 1; $i++)
{
// Initialize min and max for all
// subarrays starting with i
$mn = $arr[$i];
$mx = $arr[$i];
// Consider all subarrays starting
// with i and ending with j
for ($j = $i + 1; $j < $n; $j++)
{
// Update min and max in this
// subarray if needed
$mn = mins($mn, $arr[$j]);
$mx = maxi($mx, $arr[$j]);
// If current subarray has all
// contiguous elements
if (($mx - $mn) == $j - $i)
$max_len = maxi($max_len,
$mx - $mn + 1);
}
}
return $max_len; // Return result
}
// Driver Code
$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));
// This code is contributed
// by Shivi_Aggarwal.
?>
JavaScript
<script>
// Utility functions to find minimum
// and maximum of two elements
function min( x, y) { return (x < y)? x : y; }
function max( x, y) { return (x > y)? x : y; }
// Returns length of the longest
// contiguous subarray
function findLength( arr, n)
{
let max_len = 1; // Initialize result
for (let i=0; i<n-1; i++)
{
// Initialize min and max for all
// subarrays starting with i
let mn = arr[i], mx = arr[i];
// Consider all subarrays starting
// with i and ending with j
for (let j=i+1; j<n; j++)
{
// Update min and max in this
// subarray if needed
mn = min(mn, arr[j]);
mx = max(mx, arr[j]);
// If current subarray has all
// contiguous elements
if ((mx - mn) == j-i)
max_len = Math.max(max_len, mx-mn+1);
}
}
return max_len; // Return result
}
// driver code
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
Similar Reads
Length of the largest subarray with contiguous elements | Set 2 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 arr
7 min read
Length of Longest Subarray with same elements in atmost K increments Given an integer array arr and a number K, the task is to find the length of the longest subarray such that all the elements in this subarray can be made the same in atmost K increments.Examples: Input: arr[] = {2, 0, 4, 6, 7}, K = 6 Output: 3 The longest subarray is {2, 0, 4} which can be made as {
15+ min read
Longest subarray with all elements same Given an array arr[] of size N, the task is to find the largest subarray which consists of all equal elements.Examples: Input: arr[] = {1, 1, 2, 2, 2, 3, 3}; Output: 3 Explanation: Longest subarray with equal elements is {2, 2, 2}Input: arr[] = {1, 1, 2, 2, 2, 3, 3, 3, 3}; Output: 4 Explanation: Lon
4 min read
Query to find length of the longest subarray consisting only of 1s Given a binary array arr[] of size N and a 2D array Q[][] containing K queries of the following two types: 1 : Print the length of the longest subarray consisting of 1s only.2 X : Flip the element at the Xth index (1-based indexing) i.e change the element to '1' if the current element is '0' and vic
15+ min read
Longest subarray such that adjacent elements have at least one common digit | Set - 2 Given an array of N integers, the task is to find the length of the longest subarray such that adjacent elements of the subarray have at least one digit in common.Examples: Input : arr[] = {12, 23, 45, 43, 36, 97} Output : 3 Explanation: The subarray is 45 43 36 which has 4 common in 45, 43 and 3 co
12 min read