2206. Divide Array Into Equal Pairs - Detailed Explanation

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

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

  1. Frequency Counting:
    Since every element must have a matching pair, each distinct element in the array must appear an even number of times.

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

  1. Count Frequencies:
    Traverse the array and count the frequency of each element.
  2. Check Even Occurrence:
    For each distinct element, if its frequency is odd, then it’s impossible to pair all occurrences. In that case, return false. Otherwise, if all frequencies are even, return true.

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

  1. Sort the Array:
    Sorting the array will place identical elements next to each other.
  2. Check Adjacent Elements:
    Iterate over the array in steps of 2 and verify that nums[i] is equal to nums[i+1] for every even index i.
  3. Return Result:
    If any adjacent pair does not match, return false. Otherwise, return true.

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)

Python3
Python3

. . . .

Python Implementation (Sorting Approach)

Python3
Python3

. . . .

Java Implementation (Frequency Counting Approach)

Java
Java

. . . .

Java Implementation (Sorting Approach)

Java
Java

. . . .

Step-by-Step Walkthrough and Visual Example

Let’s illustrate the frequency counting approach with Example 1 (nums = [3, 2, 3, 2]):

  1. Count Frequency:
    • Frequency of 3 is 2.
    • Frequency of 2 is 2.
  2. Check Frequency:
    • Both numbers appear an even number of times (2 each).
  3. Result:
    Since every number appears an even number of times, it is possible to pair identical numbers. Thus, the answer is true.

For the sorting approach with the same example:

  1. Sort the Array:
    The sorted array is [2,2,3,3].
  2. Check Pairs:
    • Compare the first pair: 2 and 2 are equal.
    • Compare the second pair: 3 and 3 are equal.
  3. Result:
    All pairs consist of equal numbers, so the answer is true.

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.

TAGS
leetcode
CONTRIBUTOR
Design Gurus Team
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Related Courses
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
Image
One-Stop Portal For Tech Interviews.
Copyright © 2025 Design Gurus, LLC. All rights reserved.
;