Skip to content

Commit d817c49

Browse files
committed
Add: Exercise 1679. Max Number of K-Sum Pairs
1 parent d269b70 commit d817c49

File tree

5 files changed

+103
-3
lines changed

5 files changed

+103
-3
lines changed
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# 1679. Max Number of K-Sum Pairs
2+
Given an integer array `nums` and an integer `k`, the task is to find the maximum number of
3+
operations where each operation consists of removing a pair of numbers from the array that add up to
4+
`k`.
5+
6+
## Examples
7+
8+
### Example 1:
9+
Input: `nums` = [1,2,3,4], `k` = 5
10+
11+
Output: 2
12+
13+
Explanation:
14+
- Remove numbers 1 and 4, making `nums` = [2,3].
15+
- Remove numbers 2 and 3.
16+
17+
After this, there are no more pairs that sum up to 5, resulting in a total of 2 operations.
18+
19+
### Example 2:
20+
Input: `nums` = [3,1,3,4,3], `k` = 6
21+
22+
Output: 1
23+
24+
Explanation:
25+
- Remove the first two 3's, making `nums` = [1,4,3].
26+
27+
There are no more pairs that sum up to 6, hence a total of 1 operation.
28+
29+
## Constraints
30+
- 1 <= `nums.length` <= 10^5
31+
- 1 <= `nums[i]` <= 10^9
32+
- 1 <= `k` <= 10^9
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Finds the maximum number of k-sum pairs in the array.
3+
* @param {number[]} nums - The array of numbers.
4+
* @param {number} k - The target sum for the pairs.
5+
* @return {number} - The maximum number of k-sum pairs.
6+
*/
7+
var maxOperations = function(nums, k) {
8+
const req = {}; // Object to store the required numbers to form a pair with each element.
9+
let maxCount = 0; // Counter for the maximum number of k-sum pairs.
10+
11+
for (const n of nums) {
12+
const complement = k - n; // Calculate the complement number needed to reach k.
13+
14+
// If the complement exists in req, a pair is formed.
15+
if (req[complement] >= 1) {
16+
req[complement]--; // Decrement the count of the complement as it's now used.
17+
maxCount++; // Increment the count of pairs.
18+
} else {
19+
// If the complement is not found, record the current number.
20+
req[n] = (req[n] || 0) + 1;
21+
}
22+
}
23+
24+
return maxCount; // Return the total number of pairs formed.
25+
};
26+
27+
// Example usage
28+
// var m = maxOperations([3,1,3,4,3], 6);
29+
30+
module.exports = maxOperations
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "1679-max-number-of-k-sum-pairs",
3+
"version": "1.0.0",
4+
"description": "Given an integer array `nums` and an integer `k`, the task is to find the maximum number of operations where each operation consists of removing a pair of numbers from the array that add up to `k`.",
5+
"main": "index.js",
6+
"scripts": {
7+
"start": "node index.js",
8+
"debug": "node --inspect-brk index.js",
9+
"test": "node test.js"
10+
},
11+
"keywords": [],
12+
"author": "",
13+
"license": "ISC"
14+
}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const assert = require('assert')
2+
const maxOperations = require('./index')
3+
4+
const tests = [
5+
{
6+
nums: [1,2,3,4],
7+
k: 5,
8+
expected: 2,
9+
},
10+
{
11+
nums: [3,1,3,4,3],
12+
k: 6,
13+
expected: 1,
14+
},
15+
]
16+
17+
for (const t of tests) {
18+
assert.strict.deepEqual(maxOperations(t.nums, t.k), t.expected)
19+
}

README.md

+8-3
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,16 @@ You can use the following scripts on each exercise:
1717
{
1818
// ...
1919
"scripts": {
20-
"start": "node index.js", // Starts the program
21-
"debug": "node --inspect-brk index.js", // Starts the program in debug mode
22-
"test": "node test.js" // Run the test suite for this exercise
20+
"start": "node index.js",
21+
"debug": "node --inspect-brk index.js",
22+
"test": "node test.js"
2323
},
2424
// ...
2525
}
2626

2727
```
28+
29+
Where:
30+
- `start`: Starts the program
31+
- `debug`: Starts the program in debug mode
32+
- `test`: Run the test suite for this exercise

0 commit comments

Comments
 (0)