Skip to main content

Smallest Pair With Different Frequencies - Solution & Explanation

EasyArrayHash TableCounting6 min readAsked at: Nagarro
Practice this problem

Problem Statement

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].

 

Example 1:

Input: nums = [1,1,2,2,3,4]

Output: [1,3]

Explanation:

The smallest value is 1 with a frequency of 2, and the smallest value greater than 1 that has a different frequency from 1 is 3 with a frequency of 1. Thus, the answer is [1, 3].

Example 2:

Input: nums = [1,5]

Output: [-1,-1]

Explanation:

Both values have the same frequency, so no valid pair exists. Return [-1, -1].

Example 3:

Input: nums = [7]

Output: [-1,-1]

Explanation:

There is only one value in the array, so no valid pair exists. Return [-1, -1].

 

Constraints:

  • 1 <= nums.length <= 100
  • 1 <= nums[i] <= 100

Approach Overview

Problem Overview: You are given an integer array and need to return the smallest pair of values whose frequencies in the array are different. The key idea is that the pair must contain two numbers that appear a different number of times, while still producing the smallest possible pair by value.

Approach 1: Brute Force Pair Checking (O(n²) time, O(1) extra space)

The most direct approach checks every possible pair in the array. For each pair (i, j), compute the frequency of nums[i] and nums[j] by scanning the array and compare them. If the frequencies differ, update the answer if the pair is smaller than the current best. This approach works but is inefficient because frequency computation is repeated many times. With nested loops and repeated counting, the time complexity grows to O(n²) or worse depending on how frequencies are calculated. It mainly serves as a baseline to demonstrate the need for preprocessing.

Approach 2: Hash Table Frequency Counting (O(n) time, O(n) space)

The optimal solution uses a hash table to count how often each value appears. First iterate through the array and build a frequency map using a dictionary or map structure. This preprocessing step runs in O(n) time and avoids repeated counting later.

Next, work with the unique values and their frequencies. Track candidate values while ensuring their frequencies differ. Since the goal is the smallest pair by value, you compare numbers in increasing order and choose the smallest combination where the frequency counts are not equal. The frequency comparison becomes constant time thanks to the map lookup. This reduces the overall complexity to O(n) time with O(n) additional space for the frequency map.

This approach is common in problems involving value frequency analysis. Hash-based counting allows quick lookups and eliminates repeated work. Many array problems with constraints on occurrences rely on the same pattern, often categorized under counting techniques.

Recommended for interviews: The hash table counting approach is what interviewers expect. It demonstrates that you recognize repeated work in the brute force method and eliminate it using preprocessing. Mentioning the brute force idea first shows problem exploration, but implementing the hash map solution shows practical optimization skills and familiarity with frequency-based patterns.

Solution

We use a hash table cnt to count the frequency of each value in the array. Then we find the smallest value x, and the smallest value y that is greater than x and has a different frequency from x. If no such y exists, return [-1, -1].

The time complexity is O(n), and the space complexity is O(n), where n is the length of the array nums.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force Pair CheckingO(n²)O(1)Useful for understanding the problem or when the array size is extremely small
Hash Table Frequency CountingO(n)O(n)General case. Best approach when you need fast frequency comparisons

Video Solution

3852. Smallest Pair With Different Frequencies (Leetcode Easy)Programming Live with Larry124 views views

Watch 4 more video solutions →

Frequently Asked Questions

Is Smallest Pair With Different Frequencies easy or hard?
The problem is typically classified as Easy because the main idea is simple frequency counting with a hash table. Once the counts are available, identifying a pair with different frequencies becomes straightforward.
Smallest Pair With Different Frequencies Python/Java solution
Both Python and Java implementations follow the same pattern: build a frequency map using a dictionary (Python) or HashMap (Java), then iterate through the values to identify the smallest pair with unequal frequencies. The algorithm runs in O(n) time with O(n) extra space.
How to solve Smallest Pair With Different Frequencies in O(n)?
First iterate through the array and store the count of each value in a hash map. Then examine the unique numbers and check their stored frequencies. Select the smallest pair of values whose frequency counts are not equal. Because frequency lookup is constant time, the full process stays O(n).
What is the best approach for Smallest Pair With Different Frequencies?
The best approach uses a hash table to count the frequency of each number in the array. After building the frequency map in O(n) time, compare values and select the smallest pair whose frequencies differ. Hash lookups are O(1), so the overall solution runs in linear time.
Is Smallest Pair With Different Frequencies asked at Google/Amazon/Meta?
Frequency-based array problems commonly appear in interviews at companies like Amazon, Google, and Meta. Variations that require counting elements with hash maps or dictionaries are especially popular because they test both data structure knowledge and algorithmic optimization.
What data structure is used in Smallest Pair With Different Frequencies?
A hash table (dictionary or map) is the main data structure. It stores the frequency of each number so the algorithm can compare counts instantly instead of recomputing them by scanning the array.
What is the time complexity of Smallest Pair With Different Frequencies?
The optimal solution runs in O(n) time and O(n) space. The array is scanned once to build the frequency map, and the remaining comparisons use constant-time hash lookups. A naive brute force approach would take O(n²).

Ready to solve this problem?

Practice Smallest Pair With Different Frequencies with our built-in code editor and test cases.

Practice on FleetCode