🔥 Unique Question: Maximum Rare-Window Integer
Problem:
You are given an integer array nums and an integer k.
A number x is called a rare-window integer if:
x appears in at least one subarray of size k.
x appears in exactly one such subarray.
In that subarray, x must occur exactly once.
Return the largest rare-window integer. If no such integer exists, return -1.
A subarray is a contiguous sequence of elements of nums.
Example 1:
Input:
nums = [4, 2, 9, 2, 7, 8]
k = 3
Output:
9
Explanation:
Size-3 subarrays are:
[4, 2, 9]
[2, 9, 2]
[9, 2, 7]
[2, 7, 8]
4 appears in exactly one window and occurs once → valid
9 appears in three windows → invalid
2 appears in all four windows → invalid
7 appears in two windows → invalid
8 appears in one window and occurs once → valid
Therefore, the largest valid integer is 8.
Output:
8
Example 2
Input:
nums = [5, 1, 5, 3, 7]
k = 2
Output:
7
Size-2 windows:
[5,1]
[1,5]
[5,3]
[3,7]
1 → one window, once → valid
3 → one window, once → valid
7 → one window, once → valid
5 → three windows → invalid
Largest = 7.
Example 3
Input:
nums = [6, 6, 4, 3]
k = 2
Output:
4
Windows:
[6,6]
[6,4]
[4,3]
6 appears in 2 windows → invalid
4 appears in 1 window and occurs once → valid
3 appears in 1 window and occurs once → valid
Answer = 4.
Constraints
1 <= nums.length <= 100
1 <= k <= nums.length
0 <= nums[i] <= 10^5
Difficulty: Medium
Main concepts:
Sliding Window + Frequency Counting + Hash Map

