text function threeSum(nums): sort(nums) result = [] for i from 0 to n-3: if i > 0 and nums[i] == nums[i-1]: continue // skip duplicate first element left = i + 1 right = n - 1 target = -nums[i] while left < right: sum = nums[left] + nums[right] if sum == target: result.add([nums[i], nums[left], nums[right]]) // skip duplicates while left < right and nums[left] == nums[left+1]: left++ while left < right and nums[right] == nums[right-1]: right-- left++ right-- else if sum < target: left++ else: right-- return result Time complexity: . Sorting is , then iterations with two-pointer search each. Space complexity: extra space (or depending on sort implementation).
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
$ curl repovive.com/roadmaps/maang-interview-prep/two-pointers-sliding-window/3sum-pseudocode
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█████████████████████████████████████████████████████████████████████████████████████████████