Skip to Content

3 Sum - Triplets that sum to a target with no duplicates

Home | Coding Interviews | Searching and Sorting | 3 Sum - Triplets that sum to a target with no duplicates

Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.

Notice that the solution set must not contain duplicate triplets.

class Solution:
    def threeSum(self, nums: List[int]) -> List[List[int]]: 
        nums.sort() # sorting cause we need to avoid duplicates, with this duplicates will be near to each other
        l=[]
        for i in range(len(nums)):  #this loop will help to fix the one number i.e, i
            if i>0 and nums[i-1]==nums[i]:  #skipping if we found the duplicate of i
                continue 
			
			#NOW FOLLOWING THE RULE OF TWO POINTERS AFTER FIXING THE ONE VALUE (i)
            j=i+1 #taking j pointer larger than i (as said in ques)
            k=len(nums)-1 #taking k pointer from last 
            while j<k: 
                s=nums[i]+nums[j]+nums[k] 
                if s>0: #if sum s is greater than 0(target) means the larger value(from right as nums is sorted i.e, k at right) 
				#is taken and it is not able to sum up to the target
                    k-=1  #so take value less than previous
                elif s<0: #if sum s is less than 0(target) means the shorter value(from left as nums is sorted i.e, j at left) 
				#is taken and it is not able to sum up to the target
                    j+=1  #so take value greater than previous
                else:
                    l.append([nums[i],nums[j],nums[k]]) #if sum s found equal to the target (0)
                    j+=1 
                    while nums[j-1]==nums[j] and j<k: #skipping if we found the duplicate of j and we dont need to check 
					#the duplicate of k cause it will automatically skip the duplicate by the adjustment of i and j
                        j+=1   
        return l

Posted by Jamie Meyer 7 months ago

Related Problems

Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return the combinations in any order.

The same number may be chosen from candidates an unlimited number of times. Two combinations are unique if the frequency of at least one of the chosen numbers is different.

There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai.

For example, the pair [0, 1], indicates that to take course 0 you have to first take course 1.

Return true if you can finish all courses. Otherwise, return false.

Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Return the answer in any order.

A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.

Given an m x n grid of characters board and a string word, return true if word exists in the grid.

The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once.