# Longest Consecutive Subarray

### 1. Link

<https://www.nowcoder.com/practice/eac1c953170243338f941959146ac4bf?tpId=188&&tqId=38566&rp=1&ru=/ta/job-code-high-week&qru=/ta/job-code-high-week/question-ranking>

### 2. 描述

给定无序数组arr，返回其中最长的连续序列的长度(要求值连续，位置可以不连续,例如 3,4,5,6为连续的自然数）

### 示例1

输入：

```
[100,4,200,1,3,2]
```

复制返回值：

```
4
```

复制

### 示例2

输入：

```
[1,1,1]
```

复制返回值：

```
1
```

### 3. 思路

1. 先排序
2. 如果arr\[i-1] == arr\[i]-1, 就len += 1 ,  如果arr\[i-1] == arr\[i], 就len = len   Otherwise, 就len = 1 reset
3. 更新最大的max\_len

### 4. Coding

```

class Solution:
    def MLS(self , arr ):
        # idea
        # 1. sort array
        #  iterate element in array
        #    if arr[i] == arr[i-1] +1 -> len += 1
        #    otherwise: max_len = max(len, max_len), reset len
        arr.sort()
        max_len = 1
        length =1
        for i in range(1, len(arr)):
            if arr[i-1]+1 ==arr[i]:
                length += 1
            elif arr[i-1] ==arr[i]:
                continue
            else:
                length =1
            max_len = max(max_len, length)
        return max_len
        
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://wenkangwei.gitbook.io/leetcode-notes/datastructure/array/longest-consecutive-subarray.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
