# 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 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 ```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

The idea is similar to previous post. In the previous post, we checked whether maximum value minus minimum value is equal to 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 numbers in it 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 hash, we don’t consider the current subarray.

Following is the implementation of the above idea.

## C++

 `/* CPP program to find length of the largest ` `subarray which has all contiguous elements */` `#include ` `using` `namespace` `std; ` ` `  `// This function prints all distinct elements ` `int` `findLength(``int` `arr[], ``int` `n) ` `{ ` `    ``int` `max_len = 1; ``// Inialize result ` ` `  `    ``// One by one fix the starting points ` `    ``for` `(``int` `i=0; i myset; ` `        ``myset.insert(arr[i]); ` ` `  `        ``// Initialize max and min in ` `        ``// current subarray ` `        ``int` `mn = arr[i], mx = arr[i]; ` ` `  `        ``// One by one fix ending points ` `        ``for` `(``int` `j=i+1; j

## Java

 `/* Java program to find length of the largest subarray which has ` `   ``all contiguous elements */` `import` `java.util.*; ` ` `  `class` `Main ` `{ ` `    ``// This function prints all distinct elements ` `    ``static` `int` `findLength(``int` `arr[]) ` `    ``{ ` `        ``int` `n = arr.length; ` `        ``int` `max_len = ``1``; ``// Inialize result ` ` `  `        ``// One by one fix the starting points ` `        ``for` `(``int` `i=``0``; i set = ``new` `HashSet<>(); ` `            ``set.add(arr[i]); ` ` `  `            ``// Initialize max and min in current subarray ` `            ``int` `mn = arr[i], mx = arr[i]; ` ` `  `            ``// One by one fix ending points ` `            ``for` `(``int` `j=i+``1``; j

## Python3

 `# python program to find length of the largest ` `# subarray which has all contiguous elements */ ` ` `  `# This function prints all distinct elements ` `def` `findLenght(arr, n): ` `    ``max_len ``=` `1` `    ``for` `i ``in` `range``(``0``,n ``-` `1``): ` ` `  `        ``# Create an empty hash set and  ` `        ``# add i'th element to it ` `        ``myset ``=` `set``() ` `        ``myset.add(arr[i]) ` ` `  `        ``# Initialize max and min in ` `        ``# current subarray ` `        ``mn ``=` `arr[i] ` `        ``mx ``=` `arr[i] ` `        ``for` `j ``in` `range``(i ``+` `1``,n): ` ` `  `            ``# If current element is already ` `            ``# in hash set, then this subarray ` `            ``# cannot contain contiguous elements ` `            ``if` `arr[j] ``in` `myset: ` `                ``break` ` `  ` `  `            ``# Else add current element to hash ` `            ``# set and update min, max if required. ` `            ``myset.add(arr[j]) ` `            ``mn ``=` `min``(mn, arr[j]) ` `            ``mx ``=` `max``(mx, arr[j]) ` ` `  `            ``# We have already checked for  ` `            ``# duplicates, now check for other  ` `            ``#property and update max_len ` `            ``# if needed ` `            ``if` `mx ``-` `mn ``=``=` `j ``-` `i: ` `                ``max_len ``=` `max``(max_len, mx ``-` `mn ``+` `1``) ` ` `  `    ``return` `max_len ``# Return result ` ` `  ` `  `# Driver code ` ` `  `arr ``=` `[``10``, ``12``, ``12``, ``10``, ``10``, ``11``, ``10``] ` `n ``=` `len``(arr) ` `print``(``"Length of the longest contiguous subarray is"``, ` `                                ``findLenght(arr,n)) ` ` `  `# This code is contributed by Shrikant13 `

## C#

 `/* C# program to find length of the largest  ` `subarray which has all contiguous elements */` `using` `System; ` `using` `System.Collections.Generic; ` ` `  `class` `GFG { ` `     `  `    ``// This function prints all distinct  ` `    ``// elements ` `    ``static` `int` `findLength(``int` `[]arr) ` `    ``{ ` `        ``int` `n = arr.Length; ` `        ``int` `max_len = 1; ``// Inialize result ` ` `  `        ``// One by one fix the starting points ` `        ``for` `(``int` `i = 0; i < n-1; i++) ` `        ``{ ` `             `  `            ``// Create an empty hash set and ` `            ``// add i'th element to it. ` `            ``HashSet<``int``> ``set` `= ``new` `HashSet<``int``>(); ` `            ``set``.Add(arr[i]); ` ` `  `            ``// Initialize max and min in current ` `            ``// subarray ` `            ``int` `mn = arr[i], mx = arr[i]; ` ` `  `            ``// One by one fix ending points ` `            ``for` `(``int` `j = i+1; j < n; j++) ` `            ``{ ` `                 `  `                ``// If current element is already ` `                ``// in hash set, then this subarray  ` `                ``// cannot contain contiguous ` `                ``// elements ` `                ``if` `(``set``.Contains(arr[j])) ` `                    ``break``; ` ` `  `                ``// Else add curremt element to ` `                ``// hash set and update min,  ` `                ``// max if required. ` `                ``set``.Add(arr[j]); ` `                ``mn = Math.Min(mn, arr[j]); ` `                ``mx = Math.Max(mx, arr[j]); ` ` `  `                ``// We have already cheched for  ` `                ``// duplicates, now check for  ` `                ``// other property and update ` `                ``// max_len if needed ` `                ``if` `(mx-mn == j-i) ` `                    ``max_len = Math.Max(max_len, ` `                                  ``mx - mn + 1); ` `            ``} ` `        ``} ` `         `  `        ``return` `max_len; ``// Return result ` `    ``} ` `     `  `    ``// Driver function ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `[]arr = {10, 12, 12, 10, 10, 11, 10}; ` `        ``Console.WriteLine(``"Length of the longest"` `                   ``+ ``" contiguous subarray is "` `+ ` `                               ``findLength(arr)); ` `    ``} ` `} ` ` `  `// This code is contributed by Sam007 `

Output:

`Length 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.

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.