`{"id":"bcd47c3e-0bae-41b1-8a48-6d6b1dcdcc91","name":"3 Sum - Target Sum Unique Triplet","description":"1. Given an integer array 'nums', and a 'target', return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k.\r\n2. Another thing is nums[i] + nums[j] + nums[k] == target.\r\n3. Notice that the solution set must not contain duplicate triplets.\r\n","inputFormat":"Input is managed for you.","outputFormat":"Output is managed for you.","constraints":"1. 0 &lt;= nums.length &lt;= 3000\r\n2. -10^5 &lt;= nums[i] &lt;= 10^5\r\n","sampleCode":{"cpp":{"code":"#include <bits/stdc++.h>\nusing namespace std;\n\nvector<vector<int>> twoSum(vector<int>& arr, int si, int ei, int target)\n{\n vector<vector<int>> res;\n int left = si;\n int right = ei;\n sort(arr.begin(),arr.end());\n while (left < right) {\n if (left != si && arr[left] == arr[left - 1]) {\n left++;\n continue;\n }\n\n int sum = arr[left] + arr[right];\n if (sum == target) {\n vector<int> list;\n list.push_back(arr[left]);\n list.push_back(arr[right]);\n res.push_back(list);\n\n left++;\n right--;\n } else if (sum > target) {\n right--;\n } else {\n left++;\n }\n }\n\n return res;\n}\n\nvector<vector<int>> threeSum(vector<int>& nums, int targ) {\n //write your code here\n \n}\n\nint main() {\n int n = 0;\n cin >> n;\n vector<int> arr(n);\n for (int i = 0; i < n; i++) {\n cin >> arr[i];\n }\n int target = 0;\n cin >> target;\n vector<vector<int>> res = threeSum(arr, target);\n vector<string> finalResult;\n \n for (int i = 0; i < res.size(); i++) {\n vector<int> list = res[i];\n sort(list.begin(), list.end());\n string ans = \"\";\n for (int i = 0; i < list.size(); i++) {\n int val = list[i];\n ans += to_string(val) ;\n ans+=\" \";\n }\n finalResult.push_back(ans);\n }\n sort(finalResult.begin(), finalResult.end());\n\n for (int i=0;i<finalResult.size();i++){\n cout<<finalResult[i]<<endl;\n }\n\n return 0;\n}"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n\r\n // ~~~~~~~~~~~~User Section~~~~~~~~~~~~\r\n public static List<List<Integer>> threeSum(int[] nums, int targ) {\r\n // write your code here\r\n }\r\n\r\n // ~~~~~~~~~~Input Management~~~~~~~~~~\r\n public static void main(String[] args) {\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n int[] arr = new int[n];\r\n for (int i = 0; i < n; i++) {\r\n arr[i] = scn.nextInt();\r\n }\r\n int target = scn.nextInt();\r\n List<List<Integer>> res = threeSum(arr, target);\r\n ArrayList<String> finalResult = new ArrayList<>();\r\n for (List<Integer> list : res) {\r\n Collections.sort(list);\r\n String ans = \"\";\r\n for (int val : list) {\r\n ans += val + \" \";\r\n }\r\n finalResult.add(ans);\r\n }\r\n Collections.sort(finalResult);\r\n for (String str : finalResult) {\r\n System.out.println(str);\r\n }\r\n }\r\n\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"6\r\n-1 0 1 2 -1 -4\r\n0","sampleOutput":"-1 -1 2 \r\n-1 0 1 ","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"35f2cfb0-6f25-4967-b0c9-92f2384b9260","name":"Arrays And Strings For Intermediate","slug":"arrays-and-strings-for-intermediate-732","type":0},{"id":"5e254f9b-0df1-46e1-9c7c-1223a52b14d8","name":"3 Sum - Target Sum Unique Triplet","slug":"3-sum-target-sum-unique-triplet","type":1}],"next":{"id":"1898e449-d521-4d90-8984-4797733afaeb","name":"3 Sum - Target Sum Unique Triplet","type":3,"slug":"3-sum-target-sum-unique-triplet"},"prev":{"id":"2b6c9209-952d-4d60-bbd1-46d76ce94948","name":"2 Sum - Target Sum Unique Pairs MCQ","type":0,"slug":"2-sum-target-sum-unique-pairs-mcq"}}}`

# 3 Sum - Target Sum Unique Triplet

1. Given an integer array 'nums', and a 'target', return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k. 2. Another thing is nums[i] + nums[j] + nums[k] == target. 3. Notice that the solution set must not contain duplicate triplets.

`{"id":"bcd47c3e-0bae-41b1-8a48-6d6b1dcdcc91","name":"3 Sum - Target Sum Unique Triplet","description":"1. Given an integer array 'nums', and a 'target', return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k.\r\n2. Another thing is nums[i] + nums[j] + nums[k] == target.\r\n3. Notice that the solution set must not contain duplicate triplets.\r\n","inputFormat":"Input is managed for you.","outputFormat":"Output is managed for you.","constraints":"1. 0 &lt;= nums.length &lt;= 3000\r\n2. -10^5 &lt;= nums[i] &lt;= 10^5\r\n","sampleCode":{"cpp":{"code":"#include <bits/stdc++.h>\nusing namespace std;\n\nvector<vector<int>> twoSum(vector<int>& arr, int si, int ei, int target)\n{\n vector<vector<int>> res;\n int left = si;\n int right = ei;\n sort(arr.begin(),arr.end());\n while (left < right) {\n if (left != si && arr[left] == arr[left - 1]) {\n left++;\n continue;\n }\n\n int sum = arr[left] + arr[right];\n if (sum == target) {\n vector<int> list;\n list.push_back(arr[left]);\n list.push_back(arr[right]);\n res.push_back(list);\n\n left++;\n right--;\n } else if (sum > target) {\n right--;\n } else {\n left++;\n }\n }\n\n return res;\n}\n\nvector<vector<int>> threeSum(vector<int>& nums, int targ) {\n //write your code here\n \n}\n\nint main() {\n int n = 0;\n cin >> n;\n vector<int> arr(n);\n for (int i = 0; i < n; i++) {\n cin >> arr[i];\n }\n int target = 0;\n cin >> target;\n vector<vector<int>> res = threeSum(arr, target);\n vector<string> finalResult;\n \n for (int i = 0; i < res.size(); i++) {\n vector<int> list = res[i];\n sort(list.begin(), list.end());\n string ans = \"\";\n for (int i = 0; i < list.size(); i++) {\n int val = list[i];\n ans += to_string(val) ;\n ans+=\" \";\n }\n finalResult.push_back(ans);\n }\n sort(finalResult.begin(), finalResult.end());\n\n for (int i=0;i<finalResult.size();i++){\n cout<<finalResult[i]<<endl;\n }\n\n return 0;\n}"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n\r\n // ~~~~~~~~~~~~User Section~~~~~~~~~~~~\r\n public static List<List<Integer>> threeSum(int[] nums, int targ) {\r\n // write your code here\r\n }\r\n\r\n // ~~~~~~~~~~Input Management~~~~~~~~~~\r\n public static void main(String[] args) {\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n int[] arr = new int[n];\r\n for (int i = 0; i < n; i++) {\r\n arr[i] = scn.nextInt();\r\n }\r\n int target = scn.nextInt();\r\n List<List<Integer>> res = threeSum(arr, target);\r\n ArrayList<String> finalResult = new ArrayList<>();\r\n for (List<Integer> list : res) {\r\n Collections.sort(list);\r\n String ans = \"\";\r\n for (int val : list) {\r\n ans += val + \" \";\r\n }\r\n finalResult.add(ans);\r\n }\r\n Collections.sort(finalResult);\r\n for (String str : finalResult) {\r\n System.out.println(str);\r\n }\r\n }\r\n\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"6\r\n-1 0 1 2 -1 -4\r\n0","sampleOutput":"-1 -1 2 \r\n-1 0 1 ","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"35f2cfb0-6f25-4967-b0c9-92f2384b9260","name":"Arrays And Strings For Intermediate","slug":"arrays-and-strings-for-intermediate-732","type":0},{"id":"5e254f9b-0df1-46e1-9c7c-1223a52b14d8","name":"3 Sum - Target Sum Unique Triplet","slug":"3-sum-target-sum-unique-triplet","type":1}],"next":{"id":"1898e449-d521-4d90-8984-4797733afaeb","name":"3 Sum - Target Sum Unique Triplet","type":3,"slug":"3-sum-target-sum-unique-triplet"},"prev":{"id":"2b6c9209-952d-4d60-bbd1-46d76ce94948","name":"2 Sum - Target Sum Unique Pairs MCQ","type":0,"slug":"2-sum-target-sum-unique-pairs-mcq"}}}`

Editor

# 3 Sum - Target Sum Unique Triplet

easy

1. Given an integer array 'nums', and a 'target', return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k. 2. Another thing is nums[i] + nums[j] + nums[k] == target. 3. Notice that the solution set must not contain duplicate triplets.

## Constraints

1. 0 <= nums.length <= 3000 2. -10^5 <= nums[i] <= 10^5

## Format

### Input

Input is managed for you.

### Output

Output is managed for you.

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}6 -1 0 1 2 -1 -4 0```

### Sample Output

```.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}-1 -1 2 -1 0 1 ```

Discussions

Show Discussion

Related Resources