Repeated Values and Longest Two-Distinct Subarray
Arrays & HashingAuto-graded · 23 test casesLast updated
Tests hash-based duplicate detection alongside a sliding window that finds the longest subarray containing exactly two distinct integer values.
You are given an integer array nums. Your function must compute two independent properties and return them as a two-element list [result1, result2].
Property 1 — Any Repeated Value
Return True if at least one integer value appears more than once anywhere in nums. Return False if every element is distinct (no value repeats).
Property 2 — Longest Subarray with Exactly Two Distinct Values
Find the length of the longest contiguous subarray that contains exactly two distinct integer values.
Rules and tie-breaks:
- A subarray is a contiguous slice
nums[i..j]for some0 <= i <= j < len(nums). - "Exactly two distinct values" means the set of values in the subarray has cardinality 2. A subarray whose elements are all the same (cardinality 1) does not qualify, regardless of its length.
- If multiple subarrays tie for the longest, return that shared length (no need to identify which one).
- If no subarray with exactly two distinct values exists (e.g., the array has fewer than two distinct values total), return
0.
Return Format
Return a Python list of two elements:
[result1, result2]
result1is abool(TrueorFalse).result2is anint.
Note: in Python True == 1 and False == 0, so the return type is compatible with List[int].
Constraints
1 <= len(nums) <= 10^5-10^9 <= nums[i] <= 10^9- The two properties are independent: Property 1 does not affect Property 2 and vice versa.
- You may assume the input is a list of integers (no other types).
Examples
Example 1
Input: analyze_array([1, 2, 3, 1])
Output: [True, 2]
Example 2
Input: analyze_array([1, 2, 1, 3, 3, 3])
Output: [True, 4]
Example 3
Input: analyze_array([1, 2, 1, 2])
Output: [True, 4]
Hints
Hint 1
For Property 1, compare the length of the array with the size of its set — if the set is smaller, a duplicate exists.
Hint 2
For Property 2, maintain a sliding window with left and right pointers. Store the last index at which each distinct value in the window was seen. When a third distinct value enters, evict the one whose last occurrence is earliest and move left just past that index.
Hint 3
Only update the answer when the window contains exactly two distinct values; windows with one distinct value are not counted.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Property 1 — Duplicate Detection
Convert the array to a set, which collapses all duplicates. If the set has fewer elements than the original list, at least one value appeared more than once. This is a single comparison: len(set(nums)) < len(nums). Time O(n), space O(n).
Property 2 — Longest Exactly-Two-Distinct Subarray
Use a sliding window [left, right]. A dictionary last_seen maps each distinct value currently in the window to the rightmost index where it was seen.
For each new position right:
- Record
last_seen[value] = right. - If the dictionary now has three distinct keys, one value must be evicted entirely. Choose the value whose most recent occurrence index is smallest (call it
i). Setleft = i + 1and remove that value from the dictionary. This keeps the window as long as possible because evicting the other value would pushlefteven further right. - After the eviction step, if the dictionary holds exactly two keys, the window
[left, right]is a valid two-distinct subarray. Updatebest = max(best, right - left + 1).
Windows with only one distinct value (len(last_seen) == 1) are never counted, enforcing the "exactly two" requirement.
Why the Eviction Is Correct
When a third value c arrives at right, the window must lose either a or b completely. Suppose a's last occurrence is at index i and b's is at index j, with i < j. Evicting a sets left = i + 1, giving window size right - i. Evicting b sets left = j + 1 > i + 1, giving a strictly shorter window. So evicting the value with the earlier last-seen index is always optimal, and every element in (i, right] is either b or c since a does not appear after index i.
Why Only Two-Key Windows Are Counted
After processing each right, the dictionary always holds at most two keys (after eviction). When it holds exactly two, the window is a valid candidate. When it holds one (the very first element, or after all previous values were evicted), the window has only one distinct value and is skipped. This guarantees we only consider subarrays with exactly two distinct values.
Complexity
- Time: O(n). Each element is added to the dictionary once. The eviction while-loop runs at most once per step (after one eviction the dictionary is back to two keys). The
mincall scans at most three keys, which is O(1). The duplicate check is also O(n). - Space: O(n) for the set in Property 1. The window dictionary never exceeds three entries, so it uses O(1) extra space.
Edge Cases
| Input | Result | Reason |
|---|---|---|
[5] | [False, 0] | One element: no duplicate, no two-value window |
[7, 7, 7] | [True, 0] | Duplicate present, but only one distinct value so no valid subarray |
[1, 2, 3, 4] | [False, 2] | No duplicate; every adjacent pair is a valid two-value window of length 2 |
[1, 2, 1, 2] | [True, 4] | The whole array has exactly two distinct values |
Reference Solution
from typing import List
def analyze_array(nums: List[int]) -> List[int]:
# Property 1: a duplicate exists iff the set of values is smaller than the list.
has_duplicate = len(set(nums)) < len(nums)
# Property 2: sliding window tracking last-seen index of each value in window.
last_seen = {}
left = 0
best = 0
for right, value in enumerate(nums):
last_seen[value] = right
# A third distinct value entered: evict the value whose most recent
# occurrence is earliest, then advance left past that occurrence.
while len(last_seen) > 2:
earliest_value = min(last_seen, key=last_seen.get)
left = last_seen.pop(earliest_value) + 1
# Only count windows that contain exactly two distinct values.
if len(last_seen) == 2:
best = max(best, right - left + 1)
return [has_duplicate, best]