96. Max Number of K-Sum Pairs

MediumArrayArray

Given an integer array nums and integer k, in one operation you can pick two numbers that sum to k and remove them. Return the maximum number of operations.

Input: Integer array nums and integer k.

Output: Integer — maximum number of removal operations.

Examples

Example 1
Input: [1,2,3,4],5
Output: 2
Explanation: Remove (1,4) then (2,3). 2 operations.
Example 2
Input: [3,1,3,4,3],6
Output: 1
Explanation: Remove one (3,3) pair. 1 operation.
Example 3
Input: [1,2,3,4,5,6],7
Output: 3
Explanation: Remove (1,6),(2,5),(3,4). 3 operations.

Constraints

Asked by

AmazonMetaBloombergGoogleMicrosoft
Solve this problem in the editor →