3134. Find the Median of the Uniqueness Array LeetCode Solution
In this guide, you will get 3134. Find the Median of the Uniqueness Array LeetCode Solution with the best time and space complexity. The solution to Find the Median of the Uniqueness Array problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Find the Median of the Uniqueness Array solution in C++
- Find the Median of the Uniqueness Array solution in Java
- Find the Median of the Uniqueness Array solution in Python
- Additional Resources

Problem Statement of Find the Median of the Uniqueness Array
You are given an integer array nums. The uniqueness array of nums is the sorted array that contains the number of distinct elements of all the subarrays of nums. In other words, it is a sorted array consisting of distinct(nums[i..j]), for all 0 <= i <= j < nums.length.
Here, distinct(nums[i..j]) denotes the number of distinct elements in the subarray that starts at index i and ends at index j.
Return the median of the uniqueness array of nums.
Note that the median of an array is defined as the middle element of the array when it is sorted in non-decreasing order. If there are two choices for a median, the smaller of the two values is taken.
Example 1:
Input: nums = [1,2,3]
Output: 1
Explanation:
The uniqueness array of nums is [distinct(nums[0..0]), distinct(nums[1..1]), distinct(nums[2..2]), distinct(nums[0..1]), distinct(nums[1..2]), distinct(nums[0..2])] which is equal to [1, 1, 1, 2, 2, 3]. The uniqueness array has a median of 1. Therefore, the answer is 1.
Example 2:
Input: nums = [3,4,3,4,5]
Output: 2
Explanation:
The uniqueness array of nums is [1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3]. The uniqueness array has a median of 2. Therefore, the answer is 2.
Example 3:
Input: nums = [4,3,5,4]
Output: 2
Explanation:
The uniqueness array of nums is [1, 1, 1, 1, 2, 2, 2, 3, 3, 3]. The uniqueness array has a median of 2. Therefore, the answer is 2.
Constraints:
1 <= nums.length <= 105
1 <= nums[i] <= 105
Complexity Analysis
- Time Complexity: O(n\log n)
- Space Complexity: O(n)
3134. Find the Median of the Uniqueness Array LeetCode Solution in C++
class Solution {
public:
int medianOfUniquenessArray(vector<int>& nums) {
const int n = nums.size();
const long subarryCount = n * (n + 1L) / 2;
const long medianCount = (subarryCount + 1) / 2;
int l = 1;
int r = n;
while (l < r) {
const int m = (l + r) / 2;
if (subarrayWithAtMostKDistinct(nums, m) >= medianCount)
r = m;
else
l = m + 1;
}
return l;
}
private:
// Similar to 992. Subarrays with K Different Integers
long subarrayWithAtMostKDistinct(const vector<int>& nums, int k) {
long res = 0;
unordered_map<int, int> count;
for (int l = 0, r = 0; r < nums.size(); ++r) {
if (++count[nums[r]] == 1)
--k;
while (k == -1)
if (--count[nums[l++]] == 0)
++k;
res += r - l + 1; // nums[l..r], nums[l + 1..r], ..., nums[r]
}
return res;
}
};
/* code provided by PROGIEZ */
3134. Find the Median of the Uniqueness Array LeetCode Solution in Java
class Solution {
public int medianOfUniquenessArray(int[] nums) {
final int n = nums.length;
final long subarrayCount = n * (n + 1L) / 2;
final long medianCount = (subarrayCount + 1) / 2;
int l = 1;
int r = n;
while (l < r) {
final int m = (l + r) / 2;
if (subarrayWithAtMostKDistinct(nums, m) >= medianCount)
r = m;
else
l = m + 1;
}
return l;
}
// Similar to 992. Subarrays with K Different Integers
private long subarrayWithAtMostKDistinct(int[] nums, int k) {
long res = 0;
HashMap<Integer, Integer> count = new HashMap<>();
for (int l = 0, r = 0; r < nums.length; ++r) {
if (count.merge(nums[r], 1, Integer::sum) == 1)
--k;
while (k == -1)
if (count.merge(nums[l++], -1, Integer::sum) == 0)
++k;
res += r - l + 1; // nums[l..r], nums[l + 1..r], ..., nums[r]
}
return res;
}
}
// code provided by PROGIEZ
3134. Find the Median of the Uniqueness Array LeetCode Solution in Python
class Solution:
def medianOfUniquenessArray(self, nums: list[int]):
n = len(nums)
subarrayCount = n * (n + 1) // 2
medianCount = (subarrayCount + 1) // 2
# Similar to 992. Subarrays with K Different Integers
def subarraysWithAtMostKDistinct(k: int) -> int:
res = 0
count = collections.Counter()
l = 0
for r, num in enumerate(nums):
count[num] += 1
if count[num] == 1:
k -= 1
while k < 0:
count[nums[l]] -= 1
if count[nums[l]] == 0:
k += 1
l += 1
res += r - l + 1 # nums[l..r], nums[l + 1..r], ..., nums[r]
return res
l = 1
r = n
return bisect.bisect_left(range(l, r), medianCount,
key=subarraysWithAtMostKDistinct) + l
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.