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: 4 Explanation: There are 4 good pairs (0,3), (0,4), (3,4), (2,5) 0-indexed.
Example 2:
Input: nums = [1,1,1,1] Output: 6 Explanation: Each pair in the array are good.
Example 3:
Input: nums = [1,2,3] Output: 0
Constraints:
1 <= nums.length <= 100
1 <= nums[i] <= 100
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to finding good pairs involves checking every single possible pair of numbers from the list. We compare each number to every other number in the list to see if they match. If they do, we count that as a good pair.
Here's how the algorithm would work step-by-step:
def number_of_good_pairs_brute_force(numbers):
number_of_pairs = 0
# Iterate through each number in the list
for first_index in range(len(numbers)):
# For each number, compare it with all subsequent numbers
for second_index in range(first_index + 1, len(numbers)):
# Check if the numbers at both indices are equal
if numbers[first_index] == numbers[second_index]:
# Increment the counter if a good pair is found
number_of_pairs += 1
return number_of_pairs
The most efficient way to count these pairs avoids checking every single pair. It involves counting how many times each number appears, and then using a formula to calculate the number of good pairs based on those counts.
Here's how the algorithm would work step-by-step:
def number_of_good_pairs(numbers):
number_counts = {}
for number in numbers:
if number in number_counts:
number_counts[number] += 1
else:
number_counts[number] = 1
good_pairs_count = 0
# Iterate through each unique number
for number in number_counts:
count = number_counts[number]
# Apply formula to calculate good pairs for the number.
good_pairs_count += count * (count - 1) // 2
return good_pairs_count
Case | How to Handle |
---|---|
Null or empty input array | Return 0 immediately since no pairs can be formed. |
Array with only one element | Return 0 as a pair requires at least two elements. |
Array with all elements being identical | The solution should correctly calculate the number of pairs (n*(n-1)/2) in this scenario. |
Array with a large number of elements (scalability) | Using a hash map (counting frequency) ensures O(n) time complexity, addressing scalability. |
Array containing negative numbers, zeros, and positive numbers | The hash map based solution works regardless of the range or sign of numbers. |
Array with very large integer values (potential overflow during counting) | Ensure the data type used for counting can accommodate large numbers of pairs (e.g., long in Java). |
Array with a skewed distribution of values (some values appear many times) | The frequency counting approach will still correctly calculate the number of pairs based on each value's frequency. |
Array containing duplicate 'good pairs' when using nested loops incorrectly. | The outer loop index must be less than the inner loop index (i < j) for correct pair identification. |