Skip to content

Commit d283151

Browse files
authored
Merge branch 'CodeHarborHub:main' into main
2 parents bbdc9ca + d620353 commit d283151

File tree

1 file changed

+157
-0
lines changed

1 file changed

+157
-0
lines changed
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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.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.
6+
---
7+
8+
## Problem Description
9+
10+
| Problem Statement | Solution Link | LeetCode Profile |
11+
| :---------------- | :------------ | :--------------- |
12+
| [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/) |
13+
14+
15+
## Problem Description
16+
17+
You are given an integer array nums and an integer $k$.
18+
19+
In one operation, you can pick two numbers from the array whose sum equals $k$ and remove them from the array.
20+
21+
Return the maximum number of operations you can perform on the array.
22+
23+
### Examples
24+
25+
#### Example 1
26+
27+
- **Input:** $nums = [1,2,3,4], k = 5$
28+
- **Output:** $2$
29+
- **Explanation:** Starting with nums = $[1,2,3,4]$:
30+
- Remove numbers $1$ and $4$, then nums = $[2,3]$
31+
- Remove numbers $2$ and $3$, then nums = $[]$
32+
There are no more pairs that sum up to $5$, hence a total of $2$ operations.
33+
34+
35+
#### Example 2
36+
37+
- **Input:** $nums = [3,1,3,4,3], k = 6$
38+
- **Output:** $1$
39+
- **Explanation:** Starting with nums = $[3,1,3,4,3]$:
40+
- Remove the first two $3$'s, then nums = $[1,4,3]$
41+
There are no more pairs that sum up to $6$, hence a total of $1$ operation.
42+
43+
### Constraints
44+
45+
- $1 <= nums.length <= 105$
46+
- $1 <= nums[i] <= 109$
47+
- $1 <= k <= 109$
48+
49+
50+
### Intuition
51+
52+
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.
53+
54+
### Approach
55+
56+
1. **Sorting the Array:**
57+
58+
- 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.
59+
60+
2. **Initializing Pointers and Count:**
61+
62+
- 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.
63+
64+
3. **Two-Pointer Technique:**
65+
66+
- Use the two-pointer technique within a while loop until start is less than end.
67+
- Check if the sum of elements at nums[start] and nums[end] equals the target value k.
68+
- If it does, increment count and move both pointers inward (start++ and end--).
69+
- If the sum is greater than k, decrement end to reduce the sum.
70+
- If the sum is less than k, increment start to increase the sum.
71+
72+
4. **Return Count:**
73+
74+
- After the while loop, return the final value of count, which represents the maximum number of pairs in nums that sum to k.
75+
76+
### Solution Code
77+
78+
#### Python
79+
80+
```py
81+
class Solution:
82+
def maxOperations(self, nums: List[int], k: int) -> int:
83+
nums.sort() # Sort the array in ascending order
84+
count = 0
85+
start, end = 0, len(nums) - 1 # Initialize two pointers
86+
87+
while start < end:
88+
if nums[start] + nums[end] == k:
89+
count += 1
90+
start += 1
91+
end -= 1
92+
elif nums[start] + nums[end] > k:
93+
end -= 1
94+
else:
95+
start += 1
96+
97+
return count
98+
```
99+
100+
#### Java
101+
102+
```java
103+
import java.util.Arrays;
104+
105+
class Solution {
106+
public int maxOperations(int[] nums, int k) {
107+
Arrays.sort(nums); // Sort the array in ascending order
108+
int count = 0;
109+
int start = 0, end = nums.length - 1; // Initialize two pointers
110+
111+
while (start < end) {
112+
if (nums[start] + nums[end] == k) {
113+
count++;
114+
start++;
115+
end--;
116+
} else if (nums[start] + nums[end] > k) {
117+
end--;
118+
} else {
119+
start++;
120+
}
121+
}
122+
123+
return count;
124+
}
125+
}
126+
```
127+
128+
#### C++
129+
130+
```cpp
131+
class Solution {
132+
public:
133+
int maxOperations(vector<int>& nums, int k) {
134+
std::sort(nums.begin() , nums.end());
135+
int count = 0;
136+
int start = 0;
137+
int end = nums.size() - 1;
138+
139+
while (start < end){
140+
if (nums[start] + nums[end] == k){
141+
count ++;
142+
end--;
143+
start++;
144+
}
145+
else if (nums[start] + nums[end] > k){
146+
end--;
147+
}
148+
else{start++;}
149+
}
150+
return count;
151+
}
152+
};
153+
```
154+
155+
### Conclusion
156+
157+
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)