Unit 4 Session 2 (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: First, sort the list of numbers. Then use a two-pointer technique to find pairs where one is the negative of the other, updating the largest such positive integer found.
1) Sort the list of integers.
2) Initialize two pointers at the beginning and end of the sorted list.
3) Initialize a variable to keep track of the largest valid k found, start with -1.
4) While the left pointer is less than the right pointer:
a) Calculate the sum of the values at the two pointers.
b) If the sum is less than 0, move the left pointer right to find a less negative number.
c) If the sum is more than 0, move the right pointer left to find a smaller positive number.
d) If the sum is 0, update the largest k if the current k (value at the right pointer) is greater than the current largest k.
Then move both pointers to continue searching.
5) Return the largest k found or -1 if no such pair exists.
⚠️ Common Mistakes
def findLargestK(nums):
nums.sort() # Step 1
left, right = 0, len(nums) - 1
largestK = -1 # Initialize largestK with -1
while left < right: # Ensure we do not cross pointers
sum = nums[left] + nums[right]
if sum < 0: # The negative number is too small (too negative)
left += 1
elif sum > 0: # The positive number is too large
right -= 1
else: # Found a pair
largestK = nums[right] # Update largestK
left += 1
right -= 1
return largestK