# Three Sum Closest - Leet Code Solution

September 13, 2019

## Problem Statement

Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution.

``````Example:

Given array nums = [-1, 2, 1, -4], and target = 1.
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).``````

## Solution-1

The obvious brute force algorithm. The key is to get the closest. You need to compare

``Math.abs(target - calculated_sum)``

## Solution-2

Lets look at the optimized solution.

1. Sort the array
2. Have left and right pointers.
3. Keep calculating sum of three values.
4. Compare absolute value of difference between targetSum and calculated sum.
5. If sum is lesser, need to move left pointer
6. else move right pointer.
``````public int threeSumClosest(int[] nums, int target) {
Arrays.sort(nums);

int l = nums.length;
int minDiff = Integer.MAX_VALUE;
int result = 0;
for (int i=0; i<l; i++) {
int j=i+1;
int k=l-1;

while (j < k) {
int sum = nums[i] + nums[j] + nums[k];
if (sum == target) return sum;
else if (sum < target) j++;
else k--;

if (Math.abs(target - sum) < minDiff) {
minDiff = Math.abs(target - sum);
result = sum;
}
}
}

return result;
}``````

## Similar Posts

### System Design Interview Vocabulary Notes

In this post, we will see some of the frequently used concepts/vocabulary in…

### Two Sum Problem - Leet Code Solution

Problem Statement Given an array of integers, return indices of the two numbers…

### Convert String to Integer - atoi - Leet Code Solution

Problem Statement Implement atoi which converts a string to an integer…

### Add two numbers(reverse order) link list Problem - Leet Code Solution

Problem Statement You are given two non-empty linked lists representing two non…

### Heap Sort Algorithm

This is another very useful sorting algorithm based on Heap data structure. Read…

### How to nail your Coding Interview

Here are some tips while giving your coding interviews. 1. Never try to jump to…

## Latest Posts

### Jenkins Pipeline with Jenkinsfile - How To Schedule Job on Cron and Not on Code Commit

Introduction In this post we will see following: How to schedule a job on cron…

### How to Git Clone Another Repository from Jenkin Pipeline in Jenkinsfile

Introduction There are some cases, where I need another git repository while…

### How to Fetch Multiple Credentials and Expose them in Environment using Jenkinsfile pipeline

Introduction In this post, we will see how to fetch multiple credentials and…

### Jenkins Pipeline - How to run Automation on Different Environment (Dev/Stage/Prod), with Credentials

Introduction I have an automation script, that I want to run on different…

### Jenkinsfile - How to Create UI Form Text fields, Drop-down and Run for Different Conditions

Introduction I had to write a CICD system for one of our project. I had to…

### Java Log4j Logger - Programmatically Initialize JSON logger with customized keys in json logs

Introduction Java log4j has many ways to initialize and append the desired…