You are given an integer array nums
and an integer k
.
In one operation, you can pick two numbers from the array whose sum equals k
and remove them from the array.
Return the maximum number of operations you can perform on the array.
Example 1:
Input: nums = [1,2,3,4], k = 5 Output: 2 Explanation: Starting with nums = [1,2,3,4]: - Remove numbers 1 and 4, then nums = [2,3] - Remove numbers 2 and 3, then nums = [] There are no more pairs that sum up to 5, hence a total of 2 operations.
Example 2:
Input: nums = [3,1,3,4,3], k = 6 Output: 1 Explanation: Starting with nums = [3,1,3,4,3]: - Remove the first two 3's, then nums = [1,4,3] There are no more pairs that sum up to 6, hence a total of 1 operation.
Constraints:
1 <= nums.length <= 105
1 <= nums[i] <= 109
1 <= k <= 109
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:
We need to find pairs of numbers from a collection that add up to a specific target number. The brute force method is simple: we'll just check every possible pair to see if it meets the criteria.
Here's how the algorithm would work step-by-step:
def max_number_of_k_sum_pairs_brute_force(numbers, target):
number_of_pairs = 0
# Iterate through each number in the list.
for first_number_index in range(len(numbers)):
# To avoid double counting, we only check pairs after the current index
for second_number_index in range(first_number_index + 1, len(numbers)):
if numbers[first_number_index] + numbers[second_number_index] == target:
number_of_pairs += 1
# Return the total number of k-sum pairs.
return number_of_pairs
The best way to solve this problem is to efficiently find pairs that add up to the target number. We can do this by looking at the numbers one by one and quickly checking if their 'partner' exists.
Here's how the algorithm would work step-by-step:
def max_number_of_k_sum_pairs(numbers, target):
number_counts = {}
for number in numbers:
number_counts[number] = number_counts.get(number, 0) + 1
number_of_pairs = 0
for number in list(number_counts.keys()):
complement = target - number
# Only proceed if the complement exists
if complement in number_counts:
# Need to handle the case where the number and complement are the same
if number == complement:
pairs_formed = number_counts[number] // 2
number_of_pairs += pairs_formed
number_counts[number] -= pairs_formed * 2
# Handle cases where the number and complement are distinct
else:
pairs_formed = min(number_counts[number], number_counts[complement])
# Update pair counts and remove used numbers
number_of_pairs += pairs_formed
number_counts[number] -= pairs_formed
number_counts[complement] -= pairs_formed
return number_of_pairs
Case | How to Handle |
---|---|
Empty input array | Return 0 immediately, as no operations are possible. |
Input array with only one element | Return 0, as a pair requires at least two elements. |
k is zero and array contains zero(s) | Handle zero pairs correctly; the count of zeros must be considered carefully to avoid overcounting. |
All numbers in the array are the same, and sum equals k | Properly count pairs; Divide the count of the number by 2. |
k is negative, array has positive, negative, and zero values | The solution should correctly handle negative numbers contributing to the sum k. |
Large input array, nearing memory limit | Use an efficient data structure (e.g., hash map or frequency counter) to avoid exceeding memory constraints. |
Input array contains Integer.MAX_VALUE or Integer.MIN_VALUE, potentially leading to integer overflow when summing with k | Use a data type that can handle larger values (e.g., long) or carefully check for potential overflows before performing arithmetic operations. |
No pairs in array sum to k | Return 0, as no operations are possible when no suitable pairs are present. |