Skip to content

Instantly share code, notes, and snippets.

@noahlz
Created December 4, 2011 03:22
Show Gist options
  • Save noahlz/1429033 to your computer and use it in GitHub Desktop.
Save noahlz/1429033 to your computer and use it in GitHub Desktop.
Given an array of integers, determine if any two given elements sum to a given target.
import java.util.Arrays;
import java.util.TreeMap;
import java.util.Map;
import java.util.Random;
/**
* Given an array of integers, determine if any two given elements sum to a given target.
*/
public class FindTwoSumToTarget {
private static final Random random = new Random();
private static final int RANDOM_INT_RANGE = 20;
private static int[] newTestArray(int size) {
final int[] arr = new int[size];
for (int i = 0; i < arr.length; i++) {
int sign = random.nextInt(3);
arr[i] = random.nextInt(100) * (sign % 2 == 0 ? -1 : 1);
}
return arr;
}
public static void main(String[] args) {
int[] arr = newTestArray(20);
System.out.println("Test Array:");
System.out.println(Arrays.toString(arr));
int targetSum = random.nextInt(RANDOM_INT_RANGE) + random.nextInt(RANDOM_INT_RANGE);
boolean result = containsSum(arr, targetSum);
assert result == containsSumBrute(arr, targetSum) :
"Brute force check found a different result from the version that uses a map.";
}
private static boolean containsSum(int[] arr, int targetSum) {
// Using TreeMap just so it will display sorted.
Map<Integer, Integer> map = new TreeMap<Integer, Integer>();
for (int i : arr) {
Integer val = map.get(i);
map.put(i, val == null ? 1 : val + 1);
}
System.out.println("Analysis: " + map);
System.out.println("Target sum: " + targetSum);
for(int i : arr) {
int compliment = targetSum - i;
boolean onlyNeedsOneCount = targetSum != compliment;
if(map.containsKey(compliment)) {
if(onlyNeedsOneCount || map.get(compliment) > 1) {
System.out.println("CONFIRMED: " + i + " + " + compliment + " = " + targetSum);
return true;
}
}
}
System.out.println("No two integers in the array sum to " + targetSum);
return false;
}
private static boolean containsSumBrute(int[] arr, int target) {
for (int i = 0; i < arr.length; i++) {
int val = arr[i];
for(int j = 0; j < arr.length; j++) {
if(i != j) {
if(val + arr[j] == target) {
System.out.println("(Brute force check: " + val + " + " + arr[j] + " = " + target + ")");
return true;
}
}
}
}
System.out.println("(Brute force check confirmed no to integers in the array sum to " + target + ")");
return false;
}
}
@noahlz
Copy link
Author

noahlz commented Dec 4, 2011

Test Array:
[31, 0, -38, -56, -12, -52, -13, -24, 97, -51, 93, -25, -7, -30, -86, 92, 23, 25, -35, 37]
Analysis: {-86=1, -56=1, -52=1, -51=1, -38=1, -35=1, -30=1, -25=1, -24=1, -13=1, -12=1, -7=1, 0=1, 23=1, 25=1, 31=1, 37=1, 92=1, 93=1, 97=1}
Target sum: 9
No two integers in the array sum to 9
(Brute force check confirmed no to integers in the array sum to 9)

Test Array:
[-49, -73, 94, -74, 28, 0, -39, -37, 12, -23, -78, -40, -73, -34, -70, -49, 27, 87, -76, -67]
Analysis: {-78=1, -76=1, -74=1, -73=2, -70=1, -67=1, -49=2, -40=1, -39=1, -37=1, -34=1, -23=1, 0=1, 12=1, 27=1, 28=1, 87=1, 94=1}
Target sum: 11
CONFIRMED: 87 + -76 = 11
(Brute force check: 87 + -76 = 11)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment