Skip to content

Commit 931a348

Browse files
leetcode answer 1679
1 parent 2789cb1 commit 931a348

File tree

1 file changed

+56
-35
lines changed

1 file changed

+56
-35
lines changed

Leetcode/Leetcode_1679.py

Lines changed: 56 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,56 +3,77 @@
33
class Solution:
44
def maxOperations(self, nums: List[int], k: int) -> int:
55
"""
6-
Problem:
7-
Find the maximum number of pairs in the array such that
8-
the sum of each pair equals k. Each element can be used only once.
6+
Problem
7+
-------
8+
Given an integer array `nums` and an integer `k`, return the maximum number
9+
of operations where each operation consists of choosing two numbers whose
10+
sum equals `k`. Each number can be used only once.
911
10-
Approach (Two Pointer Technique):
11-
1. First sort the array so that we can use two pointers.
12+
Example
13+
-------
14+
Input: nums = [1,2,3,4], k = 5
15+
Output: 2
16+
Explanation:
17+
(1,4) -> 5
18+
(2,3) -> 5
19+
20+
--------------------------------------------------
21+
22+
Approach: Two Pointer Technique
23+
--------------------------------
24+
1. First sort the array so that numbers are in increasing order.
1225
2. Use two pointers:
13-
- l (left) starting from the beginning.
14-
- r (right) starting from the end.
15-
3. Check the sum of elements at these pointers.
16-
4. If sum == k → we found a valid pair:
17-
- increase pair count
18-
- move both pointers inward
19-
5. If sum < k → move the left pointer right to increase the sum.
20-
6. If sum > k → move the right pointer left to decrease the sum.
26+
- Left pointer (l) starting from the beginning.
27+
- Right pointer (r) starting from the end.
28+
3. Calculate the sum of elements at both pointers.
29+
4. If the sum equals k:
30+
- A valid pair is found.
31+
- Increase the pair count.
32+
- Move both pointers inward.
33+
5. If the sum is less than k:
34+
- Move the left pointer forward to increase the sum.
35+
6. If the sum is greater than k:
36+
- Move the right pointer backward to decrease the sum.
2137
7. Continue until the pointers meet.
2238
23-
Why this works:
24-
Sorting allows us to efficiently adjust the sum by moving pointers
25-
instead of checking all possible pairs (which would be O(n²)).
39+
Why This Works
40+
---------------
41+
Sorting allows us to efficiently adjust the sum using two pointers
42+
instead of checking every possible pair.
43+
44+
--------------------------------------------------
45+
46+
Time Complexity
47+
----------------
48+
Sorting: O(n log n)
49+
Two-pointer traversal: O(n)
2650
27-
Time Complexity (TC):
28-
Sorting takes O(n log n)
29-
Two-pointer traversal takes O(n)
3051
Overall TC = O(n log n)
3152
32-
Space Complexity (SC):
33-
O(1) → No extra data structures used (in-place operations)
53+
Space Complexity
54+
-----------------
55+
O(1) → No extra space is used (in-place operations).
3456
"""
3557

36-
pairs = 0 # Count of valid pairs
37-
nums.sort() # Sort the array to apply two-pointer technique
58+
pairs = 0
59+
nums.sort() # Sort array to apply two-pointer technique
3860

3961
l = 0 # Left pointer
4062
r = len(nums) - 1 # Right pointer
4163

42-
# Continue until the two pointers meet
64+
# Traverse until both pointers meet
4365
while l < r:
66+
current_sum = nums[l] + nums[r]
4467

45-
n = nums[l] + nums[r] # Current pair sum
46-
47-
if k == n: # If the pair sum equals k
48-
pairs += 1 # Found a valid pair
49-
l += 1 # Move left pointer forward
50-
r -= 1 # Move right pointer backward
68+
if current_sum == k: # Valid pair found
69+
pairs += 1
70+
l += 1 # Move left pointer forward
71+
r -= 1 # Move right pointer backward
5172

52-
elif n < k: # If sum is smaller than k
53-
l += 1 # Increase sum by moving left pointer
73+
elif current_sum < k: # Sum too small → increase it
74+
l += 1
5475

55-
else: # If sum is greater than k
56-
r -= 1 # Decrease sum by moving right pointer
76+
else: # Sum too large → decrease it
77+
r -= 1
5778

58-
return pairs # Return total number of valid pairs
79+
return pairs

0 commit comments

Comments
 (0)