...

/

Sum of Three Values

Sum of Three Values

Try to solve the 3Sum problem.

Statement

Given an integer array, nums, find and return all unique triplets [nums[i], nums[j], nums[k]], such that i \neq j, i \neq k, and j \neq k and nums[i] + nums[j] + nums[k] ==0== 0.

Note: The order of the triplets in the output does not matter.

Constraints:

  • 33 \leq nums.length 500\leq 500

  • 103-10^3 \leq nums[i] 103\leq 10^3

Examples

Press + to interact
canvasAnimation-image
1 / 4

Understand the problem

Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:

3Sum

1

Which triplet is a valid solution for the array nums =[3,0,1,2,1,4]= [-3, 0, 1, 2, -1, -4] that sums to zero?

A)

[3,0,4][-3, 0, 4]

B)

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

C)

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

D)

[4,2,3][-4, 2, 3]

Question 1 of 40 attempted

Figure it out!

We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.

Sequence - Vertical
Drag and drop the cards to rearrange them in the correct sequence.

1
2
3
4
5
6
7

Try it yourself

Implement your solution in the following coding playground.

Press + to interact
Python
usercode > Solution.py
def three_sum(nums):
# Replace this placeholder return statement with your code
return [[]]
3Sum

Access this course and 1200+ top-rated courses and projects.