Find the Median of the Uniqueness Array - Problem

Imagine you have an array of integers, and you want to explore the uniqueness of every possible subarray within it. For each subarray, count how many distinct elements it contains - this creates what we call a uniqueness array.

Given an integer array nums, your task is to:

  1. Calculate the number of distinct elements for every possible subarray nums[i..j] where 0 ≤ i ≤ j < nums.length
  2. Sort these uniqueness counts in non-decreasing order
  3. Find the median of this sorted uniqueness array

Example: For nums = [1, 2, 3], the subarrays and their distinct counts are:

  • [1] → 1 distinct element
  • [2] → 1 distinct element
  • [3] → 1 distinct element
  • [1,2] → 2 distinct elements
  • [2,3] → 2 distinct elements
  • [1,2,3] → 3 distinct elements

The uniqueness array becomes [1,1,1,2,2,3], and the median is 1 (the smaller middle value).

Note: When there are two middle elements, choose the smaller one.

Input & Output

example_1.py — Basic Case
$ Input: nums = [1, 2, 3]
Output: 1
💡 Note: Subarrays: [1]→1, [2]→1, [3]→1, [1,2]→2, [2,3]→2, [1,2,3]→3. Uniqueness array: [1,1,1,2,2,3]. Median of 6 elements is the smaller of positions 3,4 = 1.
example_2.py — Duplicates
$ Input: nums = [3, 4, 3, 4, 5]
Output: 2
💡 Note: Many subarrays will have repeated elements, so uniqueness values will be lower. The median uniqueness considering all 15 subarrays is 2.
example_3.py — Single Element
$ Input: nums = [1]
Output: 1
💡 Note: Only one subarray [1] with 1 distinct element. The uniqueness array is [1], so median is 1.

Constraints

  • 1 ≤ nums.length ≤ 105
  • 1 ≤ nums[i] ≤ 105
  • The uniqueness array will always have an odd number of elements or use the smaller middle value for even length

Visualization

Tap to expand
📚 Library Diversity AnalysisLibrary Shelf: Different Book GenresFiction📖Science🔬History📜Fiction📖Analyzing All Possible Shelf Sections:Section [📖] → 1 genre (Fiction)Section [🔬] → 1 genre (Science)Section [📜] → 1 genre (History)Section [📖] → 1 genre (Fiction)Section [📖🔬] → 2 genresSection [🔬📜] → 2 genresSection [📜📖] → 2 genresAnd more...Diversity Scores: [1,1,1,1,2,2,2,2,2,3]Sorted Diversity Array📊 Median Diversity: 2 genresThis represents the "typical" diversity level across all shelf sections
Understanding the Visualization
1
Survey All Sections
Look at every possible continuous shelf section [i,j] and count unique book genres
2
Collect Diversity Scores
Record the diversity (distinct genres) for each section: some sections have 1 genre, others have 2, 3, etc.
3
Find Typical Diversity
Sort all diversity scores and find the median - this represents the 'typical' diversity level
4
Optimize with Smart Counting
Instead of checking every section manually, use binary search to guess the median and verify efficiently
Key Takeaway
🎯 Key Insight: Instead of manually checking every shelf section (O(n³)), we can binary search for the median diversity level and use sliding window to efficiently verify our guess (O(n² log n))!
Asked in
Google 32 Meta 28 Amazon 15 Microsoft 12
23.4K Views
Medium Frequency
~35 min Avg. Time
856 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