3Sum Problem and Solution(LeetCode 15) | Neetcode 12 / 150 | Bharath Chandra (Telugu)

Поділитися
Вставка
  • Опубліковано 24 січ 2025

КОМЕНТАРІ • 17

  • @EnglishBytesForTelugu
    @EnglishBytesForTelugu 6 місяців тому +4

    Pseudo Code lo While J

    • @bharathh_chandraa
      @bharathh_chandraa  6 місяців тому +5

      Thank you so much for pointing this out! Pseudocode line number 7 lo J

  • @bhargav433
    @bhargav433 4 місяці тому

    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]]

  • @bhargav433
    @bhargav433 4 місяці тому

    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

  • @devfusion-8718
    @devfusion-8718 6 місяців тому +2

    Bro Please Continue This Series Don't Stop
    I Saw A Message In Your Broadcast Channel Message Is Coding Ki Nenu Best Person Kaakapovachu

    • @bharathh_chandraa
      @bharathh_chandraa  6 місяців тому

      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!!

    • @gauthamsv7628
      @gauthamsv7628 4 місяці тому

      @@bharathh_chandraa Enti bro assala videos pettadam ledu ..... Ne valle Leetcode cheyachu anna confidence vachindi .... Please dont stop it !!!

  • @VenkataSaiM-xe5ki
    @VenkataSaiM-xe5ki 5 місяців тому

    Bro One small doubt in pseudo code line 13 Is it K -= 1 or k+=1

  • @tradeiteasyy
    @tradeiteasyy 6 місяців тому

    Problem lo asal k++ chese scenario untundha🙄.....k start ayyedhe n-1 nundi kada ...so k-- a cheyyali ga?

    • @beast2811
      @beast2811 5 місяців тому

      k-- ey bro akkkda konchem mistake anthey

  • @enjoyeducation498
    @enjoyeducation498 6 місяців тому +2

    Anna coding chesedi kuda petu pls

  • @asusrog8238
    @asusrog8238 6 місяців тому +1

    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]

  • @balu9949
    @balu9949 6 місяців тому

    Good job

  • @asusrog8238
    @asusrog8238 6 місяців тому +1

    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]

  • @latestbuzzz1219
    @latestbuzzz1219 6 місяців тому

    Dp meedha problems nkaa theory chepandi

  • @vaneetha_here_navaneetha
    @vaneetha_here_navaneetha 6 місяців тому

    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