[LeetCode] Smallest Pair With Different Frequencies

3852. Smallest Pair With Different Frequencies

You are given an integer array nums.

Consider all pairs of distinct values x and y from nums such that:

  • x < y
  • x and y have different frequencies in nums.

Among all such pairs:

  • Choose the pair with the smallest possible value of x.
  • If multiple pairs have the same x, choose the one with the smallest possible value of y.

Return an integer array [x, y]. If no valid pair exists, return [-1, -1].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public:
vector<int> minDistinctFreqPair(vector<int>& nums) {
unordered_map<int,int> freq;
for(auto& x : nums) freq[x]++;
unordered_map<int,int> best;
for(auto& [k,v] : freq) {
if(!best.count(v)) best[v] = INT_MAX;
best[v] = min(best[v], k);
}
if(best.size() == 1) return {-1,-1};
vector<int> res;
for(auto& [k,v] : best) res.push_back(v);
sort(begin(res), end(res));
while(res.size() > 2) res.pop_back();
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2026/09/04/PS/LeetCode/smallest-pair-with-different-frequencies/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.