Solvarch
DSA ProblemsProgressPlanRoadmapsReviewCheat SheetsCompaniesSystem DesignCS FundamentalsQuestion Bank
Theme
Problems

3Sum

Medium
two-pointersarraysorting

Given an integer array nums, find all unique triplets [a, b, c] such that a + b + c = 0.

Input: one line of space-separated integers. Output: each triplet on its own line with its three numbers in non-decreasing order. Triplets may be produced in any order — the template prints them in a canonical order. Print nothing if there are none.

Example 1
Input:
-1 0 1 2 -1 -4
Output:
-1 -1 2
-1 0 1

The distinct zero-sum triplets are (-1,-1,2) and (-1,0,1).

Example 2
Input:
0 1 1
Output:

No triplet sums to 0.

Constraints

  • 3 ≤ nums.length ≤ 3000
  • -10^5 ≤ nums[i] ≤ 10^5
Watch a video walkthrough

Run your code against the sample tests, or Submit to run the full suite.