1512. Number of Good Pairs - Explanation
Description
You are given an array of integers nums, return the number of good pairs.
A pair (i, j) is called good if nums[i] == nums[j] and i < j.
Example 1:
Input: nums = [1,2,3,1,1,3]
Output: 4Explanation: There are 4 good pairs (0,3), (0,4), (3,4), (2,5) 0-indexed.
Example 2:
Input: nums = [1,1,1,1]
Output: 6Explanation: Each pair in the array are good.
Example 3:
Input: nums = [1,2,3]
Output: 0Constraints:
1 <= nums.length <= 1001 <= nums[i] <= 100
Topics
Prerequisites
Before attempting this problem, you should be comfortable with:
- Hash Maps - Using dictionaries to count frequencies of elements in O(1) time
- Combinatorics - Understanding how to count pairs using the formula n*(n-1)/2 for choosing 2 items from n
1. Brute Force
Intuition
A good pair is defined as a pair (i, j) where i < j and nums[i] == nums[j]. The simplest approach is to check every possible pair of indices and count those that satisfy both conditions.
Algorithm
- Initialize a counter
resto zero. - Use two nested loops: the outer loop picks index
i, and the inner loop picks indexjwherej > i. - For each pair, if
nums[i] == nums[j], incrementres. - Return
res.
Time & Space Complexity
- Time complexity:
- Space complexity:
2. Hash Map (Math)
Intuition
If a value appears c times, the number of good pairs using that value equals the number of ways to choose 2 indices from c positions, which is c * (c - 1) / 2. We can count frequencies first, then sum up the pairs for each value.
Algorithm
- Count the frequency of each number using a hash map.
- For each frequency
c, addc * (c - 1) / 2to theres. - Return the total sum.
Time & Space Complexity
- Time complexity:
- Space complexity:
3. Hash Map
Intuition
Instead of counting all frequencies first and then computing pairs, we can count pairs on the fly. As we traverse the array, each new occurrence of a value can form a good pair with every previous occurrence of that same value. We track the count of each value seen so far and add it to the res before updating the count.
Algorithm
- Initialize a hash map to store the count of each number seen so far.
- For each number in the array:
- Add the current count of that number to the
res(this is the number of new pairs formed). - Increment the count of that number in the map.
- Add the current count of that number to the
- Return the
res.
Time & Space Complexity
- Time complexity:
- Space complexity:
Common Pitfalls
Double Counting Pairs
A good pair requires i < j, meaning each pair should only be counted once. When using nested loops, ensure the inner loop starts from i + 1 rather than 0. Similarly, when using the mathematical formula c * (c - 1) / 2, this already accounts for unique pairs and should not be doubled.
Integer Overflow in Pair Calculation
When using the formula c * (c - 1) / 2 to calculate the number of pairs for a frequency c, the multiplication can overflow for large values if using 32-bit integers. In languages with fixed-size integers, ensure you use an appropriate data type or perform the division before the multiplication becomes too large.
Sign in to join the discussion