Length of the Longest Subarray with Contiguous Elements

Last Updated : 26 Sep, 2026

Given an array arr[] 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: 3
Explanation: The subarray [10, 12, 11] can be rearranged into the continuous sequence [10, 11, 12] of length 3.

Input: arr[] = [14, 12, 11, 20]
Output: 2
Explanation: The subarray [12, 11] can be rearranged into the continuous sequence [11, 12] of length 2.

Input: arr[] = [1, 56, 58, 57, 90, 92, 94, 93, 91, 45]
Output: 5
Explanation: The subarray [90, 92, 94, 93, 91] can be rearranged into the continuous sequence [90, 91, 92, 93, 94] of length 5.


[Expected Approach] Optimized Subarray Traversal - O(n ^ 2) Time and O(1) Space

Since all elements are distinct, a subarray from index i to j can form a continuous sequence if and only if the difference between its maximum and minimum equals the difference between their indices (max - min == j - i).

We can iterate through all possible starting points i of the subarray, and as we expand the ending point j, we dynamically track the minVal and maxVal. If the condition maxVal - minVal == j - i is met, we update our maximum length with j - i + 1.

C++
#include <iostream>
using namespace std;

int findLength(vector<int>& arr) {
    int maxLen = 0;
    int n = arr.size();
    for (int i = 0; i < n; i++) {
        int minVal = arr[i], maxVal = arr[i];
        for (int j = i; j < n; j++) {
            minVal = min(minVal, arr[j]);
            maxVal = max(maxVal, arr[j]);
            if (maxVal - minVal == j - i) {
                maxLen = max(maxLen, j - i + 1);
            }
        }
    }
    return maxLen;
}

int main() {
    vector<int> arr1 = {10, 12, 11};
    cout << findLength(arr1) << endl;
    
    vector<int> arr2 = {14, 12, 11, 20};
    cout << findLength(arr2) << endl;
    
    vector<int> arr3 = {1, 56, 58, 57, 90, 92, 94, 93, 91, 45};
    cout << findLength(arr3) << endl;
    
    return 0;
}
Java
class GFG {
    public static int findLength(int[] arr) {
        int maxLen = 0;
        int n = arr.length;
        for (int i = 0; i < n; i++) {
            int minVal = arr[i], maxVal = arr[i];
            for (int j = i; j < n; j++) {
                minVal = Math.min(minVal, arr[j]);
                maxVal = Math.max(maxVal, arr[j]);
                if (maxVal - minVal == j - i) {
                    maxLen = Math.max(maxLen, j - i + 1);
                }
            }
        }
        return maxLen;
    }
    
    public static void main(String[] args) {
        int[] arr1 = {10, 12, 11};
        System.out.println(findLength(arr1));
        
        int[] arr2 = {14, 12, 11, 20};
        System.out.println(findLength(arr2));
        
        int[] arr3 = {1, 56, 58, 57, 90, 92, 94, 93, 91, 45};
        System.out.println(findLength(arr3));
    }
}
Python
def findLength(arr: list[int]) -> int:
    max_len = 0
    n = len(arr)
    for i in range(n):
        min_val = arr[i]
        max_val = arr[i]
        for j in range(i, n):
            min_val = min(min_val, arr[j])
            max_val = max(max_val, arr[j])
            if max_val - min_val == j - i:
                max_len = max(max_len, j - i + 1)
    return max_len

if __name__ == "__main__":
    arr1 = [10, 12, 11]
    print(findLength(arr1))

    arr2 = [14, 12, 11, 20]
    print(findLength(arr2))
    
    arr3 = [1, 56, 58, 57, 90, 92, 94, 93, 91, 45]
    print(findLength(arr3))
C#
using System;

class GFG {
    public static int findLength(int[] arr) {
        int maxLen = 0;
        int n = arr.Length;
        for (int i = 0; i < n; i++) {
            int minVal = arr[i], maxVal = arr[i];
            for (int j = i; j < n; j++) {
                minVal = Math.Min(minVal, arr[j]);
                maxVal = Math.Max(maxVal, arr[j]);
                if (maxVal - minVal == j - i) {
                    maxLen = Math.Max(maxLen, j - i + 1);
                }
            }
        }
        return maxLen;
    }

    public static void Main(string[] args) {
        int[] arr1 = {10, 12, 11};
        Console.WriteLine(findLength(arr1));

        int[] arr2 = {14, 12, 11, 20};
        Console.WriteLine(findLength(arr2));
        
        int[] arr3 = {1, 56, 58, 57, 90, 92, 94, 93, 91, 45};
        Console.WriteLine(findLength(arr3));
    }
}
JavaScript
function findLength(arr) {
    let maxLen = 0;
    let n = arr.length;
    for (let i = 0; i < n; i++) {
        let minVal = arr[i], maxVal = arr[i];
        for (let j = i; j < n; j++) {
            minVal = Math.min(minVal, arr[j]);
            maxVal = Math.max(maxVal, arr[j]);
            if (maxVal - minVal === j - i) {
                maxLen = Math.max(maxLen, j - i + 1);
            }
        }
    }
    return maxLen;
}

// Driver Code
const arr1 = [10, 12, 11];
console.log(findLength(arr1));

const arr2 = [14, 12, 11, 20];
console.log(findLength(arr2));

const arr3 = [1, 56, 58, 57, 90, 92, 94, 93, 91, 45];
console.log(findLength(arr3));

Output
3
2
5

[Alternate Approach] Hash Set - Works for Duplicates As Well - O(n ^ 2) Time and O(n) Space

The idea is to iterate through all possible starting points of a subarray while maintaining a Hash Set to track elements and dynamically update the minimum (minVal) and maximum (maxVal) values.

While the problem states that input numbers are distinct, incorporating a hash set makes the logic robust enough to handle arrays containing duplicate elements as well (by breaking early if a duplicate is encountered). A subarray forms a valid continuous sequence if the difference between its maximum and minimum equals the difference between their indices (maxVal - minVal == j - i).

C++
#include <iostream>
#include <vector>
#include <unordered_set>
#include <algorithm>

using namespace std;

// Function to find the length of the longest contiguous subarray
int findLength(vector<int>& arr) {
    int maxLen = 0;
    int n = arr.size();
    
    // Fix the starting point of the subarray
    for (int i = 0; i < n; i++) {
        unordered_set<int> visited;
        int minVal = arr[i], maxVal = arr[i];
        
        // Fix the ending point of the subarray
        for (int j = i; j < n; j++) {
            // If duplicate is found, break early
            if (visited.count(arr[j])) {
                break;
            }
            visited.insert(arr[j]);
            
            // Update min and max values for the current window
            minVal = min(minVal, arr[j]);
            maxVal = max(maxVal, arr[j]);
            
            // Check if max - min matches the index difference (j - i)
            if (maxVal - minVal == j - i) {
                maxLen = max(maxLen, j - i + 1);
            }
        }
    }
    return maxLen;
}

int main() {
    // Public Test Cases
    vector<int> arr1 = {10, 12, 11};
    cout << findLength(arr1) << endl;

    vector<int> arr2 = {14, 12, 11, 20};
    cout << findLength(arr2) << endl;
    
    vector<int> arr3 = {1, 56, 58, 57, 90, 92, 94, 93, 91, 45};
    cout << findLength(arr3) << endl;
    
    return 0;
}
Java
import java.util.HashSet;

class GFG {
    // Function to find the length of the longest contiguous subarray
    public static int findLength(int[] arr) {
        int maxLen = 0;
        int n = arr.length;
        
        // Fix the starting point of the subarray
        for (int i = 0; i < n; i++) {
            HashSet<Integer> visited = new HashSet<>();
            int minVal = arr[i], maxVal = arr[i];
            
            // Fix the ending point of the subarray
            for (int j = i; j < n; j++) {
                // If duplicate is found, break early
                if (visited.contains(arr[j])) {
                    break;
                }
                visited.add(arr[j]);
                
                // Update min and max for the current window
                minVal = Math.min(minVal, arr[j]);
                maxVal = Math.max(maxVal, arr[j]);
                
                // Check if the sequence condition holds true
                if (maxVal - minVal == j - i) {
                    maxLen = Math.max(maxLen, j - i + 1);
                }
            }
        }
        return maxLen;
    }

    public static void main(String[] args) {
        // Public Test Cases
        int[] arr1 = {10, 12, 11};
        System.out.println(findLength(arr1));

        int[] arr2 = {14, 12, 11, 20};
        System.out.println(findLength(arr2));
        
        int[] arr3 = {1, 56, 58, 57, 90, 92, 94, 93, 91, 45};
        System.out.println(findLength(arr3));
    }
}
Python
# Function to find the length of the longest contiguous subarray
def findLength(arr: list[int]) -> int:
    max_len = 0
    n = len(arr)
    
    # Fix the starting point of the subarray
    for i in range(n):
        visited = set()
        min_val = arr[i]
        max_val = arr[i]
        
        # Fix the ending point of the subarray
        for j in range(i, n):
            # Break if duplicate element is found
            if arr[j] in visited:
                break
            visited.add(arr[j])
            
            # Track minimum and maximum values
            min_val = min(min_val, arr[j])
            max_val = max(max_val, arr[j])
            
            # Check continuous sequence condition
            if max_val - min_val == j - i:
                max_len = max(max_len, j - i + 1)
                
    return max_len

if __name__ == "__main__":
    # Public Test Cases
    arr1 = [10, 12, 11]
    print(findLength(arr1))
    
    arr2 = [14, 12, 11, 20]
    print(findLength(arr2))

    arr3 = [1, 56, 58, 57, 90, 92, 94, 93, 91, 45]
    print(findLength(arr3))
C#
using System;
using System.Collections.Generic;

class GFG {
    // Function to find the length of the longest contiguous subarray
    public static int findLength(int[] arr) {
        int maxLen = 0;
        int n = arr.Length;
        
        // Fix the starting point of the subarray
        for (int i = 0; i < n; i++) {
            HashSet<int> visited = new HashSet<int>();
            int minVal = arr[i], maxVal = arr[i];
            
            // Fix the ending point of the subarray
            for (int j = i; j < n; j++) {
                // If duplicate element is encountered, break
                if (visited.Contains(arr[j])) {
                    break;
                }
                visited.Add(arr[j]);
                
                // Track min and max in the window
                minVal = Math.Min(minVal, arr[j]);
                maxVal = Math.Max(maxVal, arr[j]);
                
                // Verify continuous sequence property
                if (maxVal - minVal == j - i) {
                    maxLen = Math.Max(maxLen, j - i + 1);
                }
            }
        }
        return maxLen;
    }

    public static void Main(string[] args) {
        // Public Test Cases
        int[] arr1 = {10, 12, 11};
        Console.WriteLine(findLength(arr1));

        int[] arr2 = {14, 12, 11, 20};
        Console.WriteLine(findLength(arr2));
        
        int[] arr3 = {1, 56, 58, 57, 90, 92, 94, 93, 91, 45};
        Console.WriteLine(findLength(arr3));
    }
}
JavaScript
// Function to find the length of the longest contiguous subarray
function findLength(arr) {
    let maxLen = 0;
    let n = arr.length;
    
    // Fix the starting point of the subarray
    for (let i = 0; i < n; i++) {
        let visited = new Set();
        let minVal = arr[i], maxVal = arr[i];
        
        // Fix the ending point of the subarray
        for (let j = i; j < n; j++) {
            // Break if a duplicate is found
            if (visited.has(arr[j])) {
                break;
            }
            visited.add(arr[j]);
            
            // Update min and max values
            minVal = Math.min(minVal, arr[j]);
            maxVal = Math.max(maxVal, arr[j]);
            
            // Check continuous sequence condition
            if (maxVal - minVal === j - i) {
                maxLen = Math.max(maxLen, j - i + 1);
            }
        }
    }
    return maxLen;
}

// Public Test Cases
const arr1 = [10, 12, 11];
console.log(findLength(arr1));

const arr2 = [14, 12, 11, 20];
console.log(findLength(arr2));

const arr3 = [1, 56, 58, 57, 90, 92, 94, 93, 91, 45];
console.log(findLength(arr3));

Output
3
2
5
Comment