Find the Largest Almost Missing Integer - Problem

You are given an integer array nums and an integer k. An integer x is almost missing from nums if x appears in exactly one subarray of size k within nums.

Return the largest almost missing integer from nums. If no such integer exists, return -1.

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

Input & Output

Example 1 — Basic Case
$ Input: nums = [1,2,3,2,4], k = 3
Output: 4
💡 Note: Subarrays: [1,2,3], [2,3,2], [3,2,4]. Number 4 appears in exactly one subarray [3,2,4], and it's the largest such number.
Example 2 — No Almost Missing
$ Input: nums = [1,2,3,1,2,3], k = 2
Output: -1
💡 Note: All subarrays: [1,2], [2,3], [3,1], [1,2], [2,3]. Every unique number appears in multiple subarrays, so no almost missing integer exists.
Example 3 — Single Element Window
$ Input: nums = [5,3,7,3,5], k = 1
Output: 7
💡 Note: Each element forms its own subarray. Number 7 appears in exactly one subarray and is the largest among such numbers.

Constraints

  • 1 ≤ nums.length ≤ 1000
  • 1 ≤ k ≤ nums.length
  • -1000 ≤ nums[i] ≤ 1000

Visualization

Tap to expand
Find the Largest Almost Missing Integernums = [1,2,3,2,4], k = 312324All Subarrays of Size 3:[1,2,3][2,3,2][3,2,4]Frequency Analysis:1: appears in 1 subarray ✓4: appears in 1 subarray ✓2: appears in 2 subarrays3: appears in 2 subarraysResult: 4
Understanding the Visualization
1
Input
Array nums and window size k
2
Process
Count subarray appearances for each number
3
Output
Largest number appearing exactly once
Key Takeaway
🎯 Key Insight: Count subarray appearances for each number and find the largest with exactly one occurrence
Asked in
Google 15 Microsoft 12
8.4K Views
Medium Frequency
~15 min Avg. Time
245 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen