3Sum LeetCode Solution in Java

Finally day 6 of #60daysOfLeetcode! Life employed some dirty tricks to delay this one but here we are. Todays question. 3sum - https://lnkd.in/d8E-SVvx Solution. We sort the array initially in order to allow use of two pointers from both ends. Then we iterate using from the beginning of the array. For each position, we check for the other the two numbers that might add up to zero using opposing two pointers, i+1 and len(arr) - 1. If the sum is too large then the right pointer reduces, otherwise the left increases. If the sum equals to zero, that combination is saved. At end we return a list of these combinations. Time complexity is O(N^2) due to the nested loop. Space complexity is O(N) due to the set. #Java #LearningInPublic #LeetCode #DSA

  • text

To view or add a comment, sign in

Explore content categories