www.fatihkabakci.com

Personal Website and Computer Science TUR EN

Interview Question Two Sum

Last update: 11/5/2016 11:16:00 AM

Solution by Fatih KABAKCI

The problem is finding a sub array which has indices of two elements in an array that if adding of corresponding their values equals a specific value. The following problem has been published on LeetCode.com. The basic one of the solution has been provided below.

Problem #1 - Two Sum

Description: Given an array of integers, return indices of the two numbers such that they add up to a specific target.

Difficulty: Easy

Solution in Java

package com.fatihkabakci.Easy;

import java.util.Scanner;

/**
 * 
 * @author fkabakci
 * Problem Description: Given an array of integers, return indices of the two numbers such that they add up to a specific target.
 * 
 * Examples:
 * 
 * Given nums = [2, 7, 11, 15], target = 9,

   Because nums[0] + nums[1] = 2 + 7 = 9,
   return [0, 1].
   
   Given nums = [0, 4, 3, 2, 0], target = 0,
   return [0, 4].
   
   Solution:
   
   Basic one,
   Brute Force Approach
 */

public class TwoSum {
	public static int[] twoSum(int[] nums, int target) {
		for (int i = 0; i < nums.length; i++) {
			for (int j = i + 1; j < nums.length; j++) {
				if (nums[i] + nums[j] == target)
					return new int[] { i, j };
			}
		}
		return null;
	}

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		System.out.println("Please input an array size");
		int n = in.nextInt();
		int[] nums = new int[n];
		System.out.println("Please fill up the array");
		for (int i = 0; i < n; i++) {
			nums[i] = in.nextInt();
		}
		System.out.println("Please input a target");
		int target = in.nextInt();

		int[] indices = twoSum(nums, target);

		if (indices != null)
			System.out.println("[" + indices[0] + "," + indices[1] + "]");
		else
			System.out.println("There are no couple of indices in the array that sum of them equals the target.");
	}
}

We try every possible each two elements of the array by adding one another to reach target by brute force approaching in the Java program above. Then, we return the array or null in regard to whether we could find two indices. The algorithm time complexity is O(n^2) because we travel for each element in two loops.

There has been no comment yet

Name:


Question/Comment
   Please verify the image




The Topics in Computer Science

Search this site for





 

Software & Algorithms

icon

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

icon

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#

Database

icon

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.

Hardware

icon

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

icon

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,Servlet, JSP,JSF, ASP

Mobile Technologies

icon

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

Network

icon

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

icon

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

icon

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.