Skip to content

Commit 098da13

Browse files
Add files via upload
1 parent 5d21a5d commit 098da13

File tree

1 file changed

+162
-0
lines changed

1 file changed

+162
-0
lines changed
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
---
2+
id: max-number-of-k-sum-pairs
3+
title: Max Number of K-Sum Pairs (Leetcode)
4+
sidebar_label: 0082-MaxNumberofK-SumPairs
5+
description: You are given an integer array nums and an integer k.
6+
7+
In one operation, you can pick two numbers from the array whose sum equals k and remove them from the array.
8+
9+
Return the maximum number of operations you can perform on the array.
10+
---
11+
12+
## Problem Description
13+
14+
| Problem Statement | Solution Link | LeetCode Profile |
15+
| :---------------- | :------------ | :--------------- |
16+
| [Max Number of K-Sum Pairs](https://leetcode.com/problems/max-number-of-k-sum-pairs/description/) | [Max Number of K-Sum Pairs Solution on LeetCode](https://leetcode.com/problems/max-number-of-k-sum-pairs/solutions) | [Aaradhya Singh ](https://leetcode.com/u/keira_09/) |
17+
18+
19+
## Problem Description
20+
21+
You are given an integer array nums and an integer $k$.
22+
23+
In one operation, you can pick two numbers from the array whose sum equals $k$ and remove them from the array.
24+
25+
Return the maximum number of operations you can perform on the array.
26+
27+
### Examples
28+
29+
#### Example 1
30+
31+
- **Input:** $nums = [1,2,3,4], k = 5$
32+
- **Output:** $2$
33+
- **Explanation:** Starting with nums = $[1,2,3,4]$:
34+
- Remove numbers $1$ and $4$, then nums = $[2,3]$
35+
- Remove numbers $2$ and $3$, then nums = $[]$
36+
There are no more pairs that sum up to $5$, hence a total of $2$ operations.
37+
38+
39+
#### Example 2
40+
41+
- **Input:** $nums = [3,1,3,4,3], k = 6$
42+
- **Output:** $1$
43+
- **Explanation:** Starting with nums = $[3,1,3,4,3]$:
44+
- Remove the first two $3$'s, then nums = $[1,4,3]$
45+
There are no more pairs that sum up to $6$, hence a total of $1$ operation.
46+
47+
### Constraints
48+
49+
- $1 <= nums.length <= 105$
50+
- $1 <= nums[i] <= 109$
51+
- $1 <= k <= 109$
52+
53+
54+
### Intuition
55+
56+
The code finds the maximum number of pairs in nums that sum to $k$ by first sorting the array and then using the two-pointer technique. Pointers start and end check pairs: if their sum equals $k$, the count is incremented and both pointers move inward. If the sum is greater than $k$, end moves left; if less, start moves right. This approach efficiently identifies pairs with a sum of $k$ in $O(nlog⁡n)$ time due to sorting and $O(n)$ time for the two-pointer traversal.
57+
58+
59+
### Approach
60+
61+
1. **Sorting the Array:**
62+
63+
- The first step is to sort the given array nums using std::sort. This step ensures that elements are in ascending order, which is crucial for the two-pointer technique.
64+
65+
2. **Initializing Pointers and Count:**
66+
67+
- Initialize three variables: count to keep track of the number of valid pairs, start pointing to the beginning of the sorted array, and end pointing to the end of the array.
68+
69+
3. **Two-Pointer Technique:**
70+
71+
- Use the two-pointer technique within a while loop until start is less than end.
72+
- Check if the sum of elements at nums[start] and nums[end] equals the target value k.
73+
- If it does, increment count and move both pointers inward (start++ and end--).
74+
- If the sum is greater than k, decrement end to reduce the sum.
75+
- If the sum is less than k, increment start to increase the sum.
76+
77+
4. **Return Count:**
78+
79+
- After the while loop, return the final value of count, which represents the maximum number of pairs in nums that sum to k.
80+
81+
### Solution Code
82+
83+
#### Python
84+
85+
```py
86+
class Solution:
87+
def maxOperations(self, nums: List[int], k: int) -> int:
88+
nums.sort() # Sort the array in ascending order
89+
count = 0
90+
start, end = 0, len(nums) - 1 # Initialize two pointers
91+
92+
while start < end:
93+
if nums[start] + nums[end] == k:
94+
count += 1
95+
start += 1
96+
end -= 1
97+
elif nums[start] + nums[end] > k:
98+
end -= 1
99+
else:
100+
start += 1
101+
102+
return count
103+
```
104+
105+
#### Java
106+
107+
```java
108+
import java.util.Arrays;
109+
110+
class Solution {
111+
public int maxOperations(int[] nums, int k) {
112+
Arrays.sort(nums); // Sort the array in ascending order
113+
int count = 0;
114+
int start = 0, end = nums.length - 1; // Initialize two pointers
115+
116+
while (start < end) {
117+
if (nums[start] + nums[end] == k) {
118+
count++;
119+
start++;
120+
end--;
121+
} else if (nums[start] + nums[end] > k) {
122+
end--;
123+
} else {
124+
start++;
125+
}
126+
}
127+
128+
return count;
129+
}
130+
}
131+
```
132+
133+
#### C++
134+
135+
```cpp
136+
class Solution {
137+
public:
138+
int maxOperations(vector<int>& nums, int k) {
139+
std::sort(nums.begin() , nums.end());
140+
int count = 0;
141+
int start = 0;
142+
int end = nums.size() - 1;
143+
144+
while (start < end){
145+
if (nums[start] + nums[end] == k){
146+
count ++;
147+
end--;
148+
start++;
149+
}
150+
else if (nums[start] + nums[end] > k){
151+
end--;
152+
}
153+
else{start++;}
154+
}
155+
return count;
156+
}
157+
};
158+
```
159+
160+
### Conclusion
161+
162+
The code effectively calculates the maximum number of pairs in an array nums that sum up to the given value $k$ using the two-pointer technique. It first sorts the array in ascending order, which enables efficient pair finding. The two pointers start and end traverse the sorted array, checking pairs. If the sum of elements at these pointers equals $k$, a valid pair is found, and both pointers move inward. If the sum is greater than $k$, end moves left to reduce the sum; if less, start moves right to increase the sum. This approach optimally identifies pairs with a sum of $k$, with a time complexity of $O(n log n)$ due to sorting and $O(n)$ for the two-pointer traversal, making it efficient for large arrays.

0 commit comments

Comments
 (0)