2831. Find the Longest Equal Subarray LeetCode Solution

In this guide, you will get 2831. Find the Longest Equal Subarray LeetCode Solution with the best time and space complexity. The solution to Find the Longest Equal Subarray problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Find the Longest Equal Subarray solution in C++
  4. Find the Longest Equal Subarray solution in Java
  5. Find the Longest Equal Subarray solution in Python
  6. Additional Resources
2831. Find the Longest Equal Subarray LeetCode Solution image

Problem Statement of Find the Longest Equal Subarray

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.

Example 1:

Input: nums = [1,3,2,3,1,3], k = 3
Output: 3
Explanation: It’s optimal to delete the elements at index 2 and index 4.
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.

Example 2:

Input: nums = [1,1,2,2,1,1], k = 2
Output: 4
Explanation: It’s optimal to delete the elements at index 2 and index 3.
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.

See also  3318. Find X-Sum of All K-Long Subarrays I LeetCode Solution

Constraints:

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

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

2831. Find the Longest Equal Subarray LeetCode Solution in C++

class Solution {
 public:
  int longestEqualSubarray(vector<int>& nums, int k) {
    int ans = 0;
    unordered_map<int, int> count;

    for (int l = 0, r = 0; r < nums.size(); ++r) {
      ans = max(ans, ++count[nums[r]]);
      while (r - l + 1 - k > ans)
        --count[nums[l++]];
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

2831. Find the Longest Equal Subarray LeetCode Solution in Java

class Solution {
  public int longestEqualSubarray(List<Integer> nums, int k) {
    int ans = 0;
    Map<Integer, Integer> count = new HashMap<>();

    for (int l = 0, r = 0; r < nums.size(); ++r) {
      ans = Math.max(ans, count.merge(nums.get(r), 1, Integer::sum));
      while (r - l + 1 - k > ans)
        count.merge(nums.get(l++), -1, Integer::sum);
    }

    return ans;
  }
}
// code provided by PROGIEZ

2831. Find the Longest Equal Subarray LeetCode Solution in Python

class Solution:
  def longestEqualSubarray(self, nums: list[int], k: int) -> int:
    ans = 0
    count = collections.Counter()

    l = 0
    for r, num in enumerate(nums):
      count[num] += 1
      ans = max(ans, count[num])
      while r - l + 1 - k > ans:
        count[nums[l]] -= 1
        l += 1

    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.