Skip to content

Instantly share code, notes, and snippets.

@silverjam
Last active December 12, 2015 08:19
Show Gist options
  • Save silverjam/4743301 to your computer and use it in GitHub Desktop.
Save silverjam/4743301 to your computer and use it in GitHub Desktop.
import java.util.*;
class Main
{
public static void sumsTo(int nums[], int sum)
{
HashMap<Integer, Integer> hash = new HashMap<Integer, Integer>();
for(int num : nums)
hash.put(num, 1);
for(int x = 0; x < nums.length; x++)
{
int y = sum - nums[x];
if (hash.containsKey(y))
System.out.println(nums[x] + " + " + y + " = " + sum);
}
}
public static void main(String args[])
{
sumsTo(new int[] { 1,2,3,4,5 }, 6 );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment