Personal Website and Computer Science TUR EN

Interview Question 3Sum Closest

Last update: 12/31/2016 7:08:00 AM


The problem is to find out the sum of three integers in an array which sum of them is closest to the target that is a given number. This problem is very similar to the problem #15 3Sum which asks the list of the sum of three integers in an array is zero. In that problem, the target is actually zero. However, this one could be any integer.

Problem #16 - 3Sum Closest

Description: Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.

Difficulty: Medium

Solution in Java

package com.fatihkabakci.Medium._3SumClosest;

import java.util.Arrays;

 * @author fkabakci
 * Problem Description: Given an array S of n integers, find three integers in S such that the sum 
 * is closest to a given number, target. Return the sum of the three integers. 
 * You may assume that each input would have exactly one solution.
 *  For example, given array S = {-1 2 1 -4}, and target = 1.
 *  The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
 * Solution:
 * 1. Sort the input array.
 * 2. Initialize closestSum as furthest of positive or negative side depending on sign of the target to make it farthest.
 * 3. Start with getting first, second and last element.
 * 4. Add up them.
 * 5. Update distances.
 * 	  Set current distance
 * 	  Set closest distance	
 * 6. If sum equals to target,
 * 		 then return it immediately.
 * 	  If sum is less than target,
 * 	     then close to greater one. (Skip to third one by increasing loop variable j)
 * 	  If sum is greater than target,
 * 		 then close to lesser one. (Skip to previous one of the last one by decreasing loop variable k)

public class _3SumClosest {	
	public int threeSumClosest(int[] nums, int target) {
		if (nums.length < 3)
			return 0;

		int closestSum = (target < 0 ) ? Integer.MIN_VALUE : Integer.MAX_VALUE;
		int i = 0, j, k;
		while (i < nums.length - 2) {
			j = i + 1;
			k = nums.length - 1;
			while (j < k) {
				int sum = nums[i] + nums[j] + nums[k];
				int distance = Math.abs(sum - target);
				int closestDistance = Math.abs(closestSum - target);
				if (distance < closestDistance) {
					closestSum = sum;

				if (sum == target)
					return sum;
				else if (sum < target) {
				} else
		return closestSum;

	public static void main(String[] args) {
		_3SumClosest c = new _3SumClosest();
		System.out.println(c.threeSumClosest(new int[]{-3, -2, -5, 3, -4}, -1));

Similar Problems

#15 3Sum

#18 4Sum

There has been no comment yet


   Please verify the image

The Topics in Computer Science

Search this site for


Software & Algorithms


In mathematics and computer science, an algorithm is a step-by-step procedure for calculations. Algorithms are used for calculation, data processing, and automated reasoning.

Programming Languages


A programming language is a formal constructed language designed to communicate instructions to a machine, particularly a computer. It can be used to create programs to control the behavior of a machine. Java,C, C++,C#



A database is an organized collection of data. The data are typically organized to model aspects of reality in a way that supports processes requiring information.



Computer hardware is the collection of physical elements that constitutes a computer system. Computer hardware refers to the physical parts or components of a computer such as the monitor, memory, cpu.

Web Technologies


Web development is a broad term for the work involved in developing a web site for the Internet or an intranet. Html,Css,JavaScript,ASP.Net,PHP are one of the most popular technologies. J2EE,Spring Boot, Servlet, JSP,JSF, ASP

Mobile Technologies


Mobile application development is the process by which application software is developed for low-power handheld devices, such as personal digital assistants, enterprise digital assistants or mobile phones. J2ME



A computer network or data network is a telecommunications network that allows computers to exchange data. In computer networks, networked computing devices pass data to each other along data connections.

Operating Systems


An operating system is software that manages computer hardware and software resources and provides common services for computer programs. The OS is an essential component of the system software in a computer system. Linux,Windows

Computer Science


Computer science is the scientific and practical approach to computation and its applications.A computer scientist specializes in the theory of computation and the design of computational systems.