In the Find the Longest Equal Subarray coding problem, you are given an array of integers and an integer . You want to find the longest possible subarray where all elements are the same, but you are allowed to delete up to elements from the original array to achieve this. You want to return the length of this "equal" subarray (the count of the identical elements).
Google and Palo Alto Networks use this problem to test your mastery of the Sliding Window interview pattern. It evaluation whether you can efficiently find the most frequent element in a window and determine if the "other" elements (the ones that would need to be deleted) exceed . It’s a sophisticated variation of the "Longest Repeating Character Replacement" problem.
This is a Sliding Window problem combined with a Hash Table.
[left, right].maxFreq which is the frequency of the most common element in the current window.(windowSize - maxFreq).left.maxFreq.nums = [1, 3, 2, 3, 1, 3], .
[1, 3, 2, 3]: size 4. Freqs: {1:1, 3:2, 2:1}. maxFreq = 2.[1, 3, 2, 3, 1, 3]: size 6. Freqs: {1:2, 3:3, 2:1}. maxFreq = 3.maxFreq only needs to be updated when a new element enters the window.Master the "Longest Subarray with Constraint" pattern. The key is usually: WindowSize - BestElementCount <= K. This allows you to find the longest segment that can be "cleaned up" into a uniform state.