1 package org.feistymeow.algorithms;
3 import java.util.HashSet;
24 boolean findSumInList(
int sum,
int list[])
26 HashSet<Integer> wanting =
new HashSet<Integer>();
27 for (
int curr : list) {
28 if (wanting.contains(curr)) {
32 wanting.add(sum - curr);
48 public static void main(String argv[])
50 SumFinder finder =
new SumFinder();
52 int list_1[] = { 7, 5, 8, 2, 9, 4, 1, 2 };
55 if (!finder.findSumInList(sum_1, list_1)) {
56 System.out.println(
"FAILURE: ON TEST CASE 1");
58 System.out.println(
"OKAY: ON TEST CASE 1");
63 int list_2[] = { 1, 9, 3, 2, 4, 4, 1 };
66 if (!finder.findSumInList(sum_2, list_2)) {
67 System.out.println(
"FAILURE: ON TEST CASE 2");
69 System.out.println(
"OKAY: ON TEST CASE 2");
74 int list_3[] = { 1, 9, 3, 2 };
77 if (finder.findSumInList(sum_3, list_3)) {
78 System.out.println(
"FAILURE: ON TEST CASE 3");
80 System.out.println(
"OKAY: ON TEST CASE 3");