> For the complete documentation index, see [llms.txt](https://coding-9.gitbook.io/untitled/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://coding-9.gitbook.io/untitled/sliding-window/2831.-find-the-longest-equal-subarray.md).

# 2831. Find the Longest Equal Subarray

## Problem Statement

<br>

You are given a **0-indexed** integer array `nums` and an integer `k`.

A subarray is called **equal** if all of its elements are equal. Note that the empty subarray is an **equal** subarray.

Return *the length of the **longest** possible equal subarray after deleting **at most*** `k` *elements from* `nums`.

A subarray is a contiguous, possibly empty sequence of elements within an array.

&#x20;

**Example 1:**

<pre><code><strong>Input: nums = [1,3,2,3,1,3], k = 3
</strong><strong>Output: 3
</strong><strong>Explanation: It's optimal to delete the elements at index 2 and index 4.
</strong>After deleting them, nums becomes equal to [1, 3, 3, 3].
The longest equal subarray starts at i = 1 and ends at j = 3 with length equal to 3.
It can be proven that no longer equal subarrays can be created.
</code></pre>

**Example 2:**

<pre><code><strong>Input: nums = [1,1,2,2,1,1], k = 2
</strong><strong>Output: 4
</strong><strong>Explanation: It's optimal to delete the elements at index 2 and index 3.
</strong>After deleting them, nums becomes equal to [1, 1, 1, 1].
The array itself is an equal subarray, so the answer is 4.
It can be proven that no longer equal subarrays can be created.
</code></pre>

&#x20;

**Constraints:**

* `1 <= nums.length <= 105`
* `1 <= nums[i] <= nums.length`
* `0 <= k <= nums.length`

## Intuition

```
Maintain a window keeping max freq of elements in that interval of Sliding window

Like for eg-
1,3,2,3,1,3
0 1 2 3 4 5

In index 0 to 3
len = 1 , max freq of any element is 2, element = 3
therefore 4-2 = 2 , which is permitted as k=3

This means that if we delete 1 and 2 in that interval, we get equal subarray

Optimal answer between interval 1 and 5
Max freq = 3 of 3
len5-freq3 = 2 < 3 Permitted

Which implies that after 2 deletions 2 and 1 , We get equal subarrray of size 3
```

### Links

<https://leetcode.com/problems/find-the-longest-equal-subarray/description/>

### Video Links

### Approach 1:

```
Sliding Window
```

{% code title="C++" lineNumbers="true" %}

```cpp
class Solution {
public:
    int longestEqualSubarray(vector<int>& arr, int k) {
        unordered_map<int,int> freq;
        int maxf = 0;
        int start = 0;

        for(int end=0; end<arr.size(); end++){
            maxf = max(maxf, ++freq[arr[end]]);

            if(end-start+1 - maxf > k){
                freq[arr[start]]--;
                start++;
            }
        }

        return maxf;
    }
};
```

{% endcode %}

### Approach 2:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Approach 3:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Approach 4:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Similar Problems

###
