2206. Divide Array Into Equal Pairs - Detailed Explanation
Problem Statement
Given an integer array nums
of even length, determine whether it is possible to divide the array into exactly n / 2
pairs such that for each pair (a, b)
, the two elements are equal. In other words, every element must be paired with another identical element.
For example, if the array is [3,2,3,2]
, you can form two pairs: (3,3)
and (2,2)
, and the answer is true
. If the array is [1,2,3,4]
, no matter how you pair the elements, at least one pair will have different values, so the answer is false
.
Examples
Example 1
- Input:
nums = [3,2,3,2]
- Output:
true
- Explanation:
You can divide the array into pairs(3,3)
and(2,2)
. All pairs consist of equal numbers.
Example 2
- Input:
nums = [1,2,3,4]
- Output:
false
- Explanation:
No valid pairing exists where every pair consists of two equal numbers. For instance, if you try pairing(1,2)
or(3,4)
, the numbers do not match.
Example 3
- Input:
nums = [1,1,2,2,3,3]
- Output:
true
- Explanation:
It is possible to form the pairs(1,1)
,(2,2)
, and(3,3)
.
Constraints
- The length of
nums
is even. - 1 ≤
nums.length
≤ 10⁵ - -10⁵ ≤
nums[i]
≤ 10⁵
Hints
-
Frequency Counting:
Since every element must have a matching pair, each distinct element in the array must appear an even number of times. -
Sorting:
Alternatively, by sorting the array, equal numbers will appear consecutively. Then, you can simply check in one pass whether every two adjacent numbers are the same.
Approaches
Approach 1: Frequency Counting Using a Hash Map
Explanation
- Count Frequencies:
Traverse the array and count the frequency of each element. - Check Even Occurrence:
For each distinct element, if its frequency is odd, then it’s impossible to pair all occurrences. In that case, returnfalse
. Otherwise, if all frequencies are even, returntrue
.
Complexity Analysis
- Time Complexity: O(n), where n is the number of elements, since we traverse the array once.
- Space Complexity: O(n) in the worst-case scenario when all elements are distinct (more precisely, O(u) where u is the number of unique elements).
Approach 2: Sorting and Pair Checking
Explanation
- Sort the Array:
Sorting the array will place identical elements next to each other. - Check Adjacent Elements:
Iterate over the array in steps of 2 and verify thatnums[i]
is equal tonums[i+1]
for every even indexi
. - Return Result:
If any adjacent pair does not match, returnfalse
. Otherwise, returntrue
.
Complexity Analysis
- Time Complexity: O(n log n) due to sorting.
- Space Complexity: O(1) if sorting is done in-place.
Code Solutions
Python Implementation (Frequency Counting Approach)
Python Implementation (Sorting Approach)
Java Implementation (Frequency Counting Approach)
Java Implementation (Sorting Approach)
Step-by-Step Walkthrough and Visual Example
Let’s illustrate the frequency counting approach with Example 1 (nums = [3, 2, 3, 2]
):
- Count Frequency:
- Frequency of
3
is 2. - Frequency of
2
is 2.
- Frequency of
- Check Frequency:
- Both numbers appear an even number of times (2 each).
- Result:
Since every number appears an even number of times, it is possible to pair identical numbers. Thus, the answer istrue
.
For the sorting approach with the same example:
- Sort the Array:
The sorted array is[2,2,3,3]
. - Check Pairs:
- Compare the first pair:
2
and2
are equal. - Compare the second pair:
3
and3
are equal.
- Compare the first pair:
- Result:
All pairs consist of equal numbers, so the answer istrue
.
Common Mistakes
-
Not Handling Negative Numbers Correctly:
Although the problem states the array has even length, ensure that counting or sorting works correctly for negative numbers if they appear. -
Overcomplicating the Problem:
The problem can be reduced to checking if every element occurs an even number of times. Avoid unnecessary simulations of pairings. -
Edge Cases:
- When the array is empty (if allowed by constraints) or when it contains only one distinct number.
- When there is one number with an odd frequency, the entire pairing fails.
Alternative Variations
-
Pairing with Different Conditions:
A variation might require pairing elements where the sum of each pair is equal to a given target. -
Minimum Swap Problems:
Another variant may ask for the minimum swaps required to group equal numbers together into pairs. -
Divide Array into Pairs with Certain Properties:
Similar problems might require pairs with differences less than a given threshold or pairs that satisfy other arithmetic conditions.
Related Problems
GET YOUR FREE
Coding Questions Catalog