Sum of Three Values
Try to solve the 3Sum problem.
We'll cover the following
Statement
Given an array of integers, nums
, and an integer value, target
, determine if there are any three integers in nums
whose sum is equal to the target
, that is, nums[i] + nums[j] + nums[k] == target
. Return TRUE if three such integers exist in the array. Otherwise, return FALSE.
Note: A valid triplet consists of elements with distinct indexes. This means, for the triplet
nums[i], nums[j]
, andnums[k]
,i
j
,i
k
andj
k
.
Constraints:
-
nums.length
-
nums[i]
-
target
Examples
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:
Sum of Three Values
What should be the output if the following set of inputs is provided?
nums = [2, 3, 1]
target = 6
True
False
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.
Try it yourself
Implement your solution in SumOfThree.java
in the following coding playground. The supporting code template provided in TwoPointers.java
is meant to assist in developing your solution to the problem.
import java.util.*;public class SumOfThree{public static boolean findSumOfThree(int[] nums, int target) {// Replace this placeholder return statement with your codereturn false;}}