Find Target Sum Triplets

Medium2s max execution

Given a list of integers and a target sum, find all unique triplets in the list that sum up to the target value. The challenge is to handle duplicates efficiently and consider various edge cases.

🎯 Problem Requirements:

  1. Input format:
    • A list of integers (positive, negative, or zero)
    • A target sum value
  2. Constraints:
    • Find all unique triplets (i,j,k) where nums[i] + nums[j] + nums[k] = target_sum
    • Each triplet should be returned in ascending order
    • The solution set must not contain duplicate triplets
    • List indices i, j, and k must be different

💡 Example:

For list [-1, 0, 1, 2, -1, -4] and target_sum = 0:

  • Valid triplets: [[-1, -1, 2], [-1, 0, 1]]
  • Note: [-1, -1, 2] uses two different -1s from the list
  • Triplets are sorted internally and in the output

📝 Your Task:

Implement the find_triplets function to find all unique triplets that sum to the target value. Consider using techniques like sorting and two-pointer approach for efficiency.

Examples:

Input: [[-1,0,1,2,-1,-4], 0]
Output: [[-1,-1,2],[-1,0,1]]

Found two unique triplets that sum to zero

Input: [[1,1,1,1], 3]
Output: [[1,1,1]]

Single triplet using three 1s from the list

Input: [[0,0,0,0], 0]
Output: [[0,0,0]]

Single triplet of zeros, duplicates handled

Constraints:

  • 0 ≤ len(nums) ≤ 3000
  • -105 ≤ nums[i] ≤ 105
  • -105 ≤ target ≤ 105
  • Solution must handle duplicates correctly

Custom Test Input

Output

Loading Python environment...