C# || 3Sum – How To Get All Triplet Combinations In Array Equal To Target Value Using C#

The following is a module with functions which demonstrates how to get all triplet combinations in an array equal to a target value using C#.


1. 3 Sum – Problem Statement

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.

Example 1:


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

Example 2:


Input: nums = []
Output: []

Example 3:


Input: nums = [0]
Output: []


2. 3 Sum – Solution

The following is a solution which demonstrates how to get all triplet combinations in an array equal to a target value.

This solution uses the two pointer technique to find combinations.

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output for the example cases:


[[-1,-1,2],[-1,0,1]]
[]
[]

Was this article helpful?
👍 YesNo

Leave a Reply