Problem:Given an array of integers, find two numbers such that they add up to the specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
For Example:
Input: number={2,7,11,15};
Output: Index1=0, Index2=1
Solution:
Using HashMap to store the target value
package com.algorithm.twosum;
import java.util.HashMap;
public class Solution {
public static int[] twoSum(int[] numbers, int target) {
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
int[] result = new int[2];
for (int i = 0; i < numbers.length; i++) {
if (map.containsKey(numbers[i])) {
int index = map.get(numbers[i]);
result[0] = index;
result[1] = i;
break;
} else {
map.put(target - numbers[i], i);
}
}
return result;
}
public static void main(String[] args) {
int[] input = { 2, 7, 11, 15 };
int[] output = twoSum(input, 9);
int count = 1;
for (int i : output) {
System.out.println("Index" + (count++) + ": " + i);
}
}
}
Time complexity depends on the put and gets operations of HashMap which is normally O(1). The time complexity of this solution is O(n).
Solution:
Here is another solution without using the HashMap. Please write a comment if anything wrong.
package com.algorithm.twosum;
public class TwoSum {
static int[] input = { 2, 7, 11, 15 };
static int target = 9;
public static void main(String[] args) {
int index = 1;
for (Integer indexes : twoSumSolution(input, target)) {
System.out.println("Index" + (index++) + ":" + indexes);
}
}
private static int[] twoSumSolution(int[] input2, int target) {
int[] output = new int[2];
int counter = 0;
boolean flag = false;
for (int i = 0, j = 1; i < input.length; i++) {
if (target == (input[i] + input[j])) {
output[0] = i;
output[1] = j;
flag = true;
}
counter++;
if (flag) {
break;
}
}
System.out.println("Total Loop Count: " + counter);
return output;
}
}
Time complexity is O(1) in the best case and O(n) in the worst case.
Two Sum || Input array is sorted
This problem is similar to Two Sum. To solve this problem, we can use two points to scan the array from both sides. See Java solution below:
Solution:
package com.algorithm.twosum;
public class TwoSum2 {
static int[] input = { 2, 7, 11, 15 };
static int target = 9;
public static void main(String[] args) {
int index = 1;
for (int indexes : twoSum(input, target)) {
System.out.println("Index" + (index++) + ":" + indexes);
}
}
public static int[] twoSum(int[] numbers, int target) {
if (numbers == null || numbers.length == 0)
return null;
int i = 0;
int j = numbers.length - 1;
while (i < j) {
int x = numbers[i] + numbers[j];
if (x < target) {
++i;
} else if (x > target) {
j--;
} else {
return new int[] { i, j };
}
}
return null;
}
}
Two Sum ||| Data Structure Design
Design and implement a TwoSum class. It should support the following operations. add and find
add – add the number to an internal data structure. find – find if there exists any pair of numbers which sum is equal to the value.
For Example:
add(1);
add(3);
add(5);
find(8); ==> True
find(6); ==> False
Solution:
Since the desired class need add and get operations, HashMap is a good option for this.
package com.algorithm.twosum;
import java.util.HashMap;
public class TwoSum3 {
private static HashMap<Integer, Integer> elements = new HashMap<Integer, Integer>();
public static void add(int number) {
if (elements.containsKey(number)) {
elements.put(number, elements.get(number) + 1);
} else {
elements.put(number, 1);
}
}
public static boolean find(int value) {
for (Integer i : elements.keySet()) {
int target = value - i;
if (elements.containsKey(target)) {
if (i == target && elements.get(target) < 2) {
continue;
}
return true;
}
}
return false;
}
public static void main(String[] args) {
add(3);
add(5);
add(6);
System.out.println("Check if value 8 exists or not? " + find(8));
System.out.println("Check if value 10 exists or not? " + find(10));
}
}
Like this:
Like Loading...