def threeSum(nums): nums.sort() # Sort the array res = [] for i in range(len(nums) - 2): # First pointer 'i' if i > 0 and nums[i] == nums[i - 1]: continue # Skip duplicates for 'i' j = i + 1 # Second pointer 'j' k = len(nums) - 1 # Third pointer 'k' while j < k: s = nums[i] + nums[j] + nums[k] # Sum of the triplet if s == 0: res.append([nums[i], nums[j], nums[k]]) # Skip duplicates for 'j' and 'k' while j < k and nums[j] == nums[j + 1]: j += 1 while j < k and nums[k] == nums[k - 1]: k -= 1 j += 1 k -= 1 elif s < 0: j += 1 # Increase 'j' to make the sum larger else: k -= 1 # Decrease 'k' to make the sum smaller return res # Example usage nums1 = [-1, 0, 1, 2, -1, -4] nums2 = [0, 1, 1] nums3 = [0, 0, 0] print(threeSum(nums1)) # Output: [[-1, -1, 2], [-1, 0, 1]] print(threeSum(nums2)) # Output: [] print(threeSum(nums3)) # Output: [[0, 0, 0]]
2nd approach(2 pointers) code in python tripletset=set() nums.sort() for i in range(len(nums)): j=i+1 k=len(nums)-1 if nums[i]>0: break rem=0-nums[i] while jrem: k-=1 else: j+=1 return [list(triplet) for triplet in tripletset]
brute force code in python n=len(nums) tripletset=set() for i in range(0,n-2): for j in range(i+1,n-1): for k in range(j+1,n): sum=nums[i]+nums[j]+nums[k] if sum==0: triplet=tuple(sorted([nums[i],nums[j],nums[k]])) tripletset.add(triplet) return [list(triplet) for triplet in tripletset]
before starting the video class Solution(object): def threeSum(self, nums): h={} for i in nums: if i in h: h[i]=h[i]+1 else: h[i]=1 for i in h: for j in h: for k in h: if i+j+k==0: return [i,j,k] i am just getting one pair only now started learning No problem
Pseudo Code lo While J
Thank you so much for pointing this out! Pseudocode line number 7 lo J
def threeSum(nums):
nums.sort() # Sort the array
res = []
for i in range(len(nums) - 2): # First pointer 'i'
if i > 0 and nums[i] == nums[i - 1]:
continue # Skip duplicates for 'i'
j = i + 1 # Second pointer 'j'
k = len(nums) - 1 # Third pointer 'k'
while j < k:
s = nums[i] + nums[j] + nums[k] # Sum of the triplet
if s == 0:
res.append([nums[i], nums[j], nums[k]])
# Skip duplicates for 'j' and 'k'
while j < k and nums[j] == nums[j + 1]:
j += 1
while j < k and nums[k] == nums[k - 1]:
k -= 1
j += 1
k -= 1
elif s < 0:
j += 1 # Increase 'j' to make the sum larger
else:
k -= 1 # Decrease 'k' to make the sum smaller
return res
# Example usage
nums1 = [-1, 0, 1, 2, -1, -4]
nums2 = [0, 1, 1]
nums3 = [0, 0, 0]
print(threeSum(nums1)) # Output: [[-1, -1, 2], [-1, 0, 1]]
print(threeSum(nums2)) # Output: []
print(threeSum(nums3)) # Output: [[0, 0, 0]]
brother we need to take range upto n-2 and we are forming triplets then we need to give iterate upto n-2 is it correct or else not
Bro Please Continue This Series Don't Stop
I Saw A Message In Your Broadcast Channel Message Is Coding Ki Nenu Best Person Kaakapovachu
Haha, the series won’t stop Manjith. Broadcast message lo Life advice ki nenu best person kaadu anna. Coding advice? Any day …. ping me on insta!!
@@bharathh_chandraa Enti bro assala videos pettadam ledu ..... Ne valle Leetcode cheyachu anna confidence vachindi .... Please dont stop it !!!
Bro One small doubt in pseudo code line 13 Is it K -= 1 or k+=1
Problem lo asal k++ chese scenario untundha🙄.....k start ayyedhe n-1 nundi kada ...so k-- a cheyyali ga?
k-- ey bro akkkda konchem mistake anthey
Anna coding chesedi kuda petu pls
2nd approach(2 pointers) code in python
tripletset=set()
nums.sort()
for i in range(len(nums)):
j=i+1
k=len(nums)-1
if nums[i]>0:
break
rem=0-nums[i]
while jrem:
k-=1
else:
j+=1
return [list(triplet) for triplet in tripletset]
Good job
brute force code in python
n=len(nums)
tripletset=set()
for i in range(0,n-2):
for j in range(i+1,n-1):
for k in range(j+1,n):
sum=nums[i]+nums[j]+nums[k]
if sum==0:
triplet=tuple(sorted([nums[i],nums[j],nums[k]]))
tripletset.add(triplet)
return [list(triplet) for triplet in tripletset]
Dp meedha problems nkaa theory chepandi
before starting the video
class Solution(object):
def threeSum(self, nums):
h={}
for i in nums:
if i in h:
h[i]=h[i]+1
else:
h[i]=1
for i in h:
for j in h:
for k in h:
if i+j+k==0:
return [i,j,k]
i am just getting one pair only
now started learning No problem