#C956. Longest Consecutive Subarray
Longest Consecutive Subarray
Longest Consecutive Subarray
Given an array of integers, your task is to find the length of the longest subarray that contains consecutive integers. Note that the integers in the subarray may appear in any order.
For example, consider the array [10, 12, 11]: Although the numbers are not in sorted order, they form the consecutive set {10, 11, 12} which has length 3.
The problem can be formally described as finding the maximum length L such that there exists a subset of the array whose elements can be arranged as a sequence of consecutive integers. Mathematically, if we denote this subset by \( S \), then the condition is:
[ \max S - \min S + 1 = |S| ]
where \(|S|\) represents the number of elements in \( S \). Your solution should handle cases including negative numbers and duplicate elements effectively.
inputFormat
The first line contains an integer \( n \) (\(0 \leq n \leq 10^5\)) representing the number of elements in the array. The second line contains \( n \) space-separated integers, which may include negative values.
outputFormat
Output a single integer representing the length of the longest subarray (i.e., subset of numbers from the array) that can form a sequence of consecutive integers.
## sample3
10 12 11
3