Skip to content

Instantly share code, notes, and snippets.

@nashid
Created October 31, 2016 14:11
Show Gist options
  • Save nashid/3b16418737a3e1d198ab95e249a5dd09 to your computer and use it in GitHub Desktop.
Save nashid/3b16418737a3e1d198ab95e249a5dd09 to your computer and use it in GitHub Desktop.
Two Sum: Given an array of integers, find two numbers such that they add up to a specific target number
import java.util.Map;
import java.util.HashMap;
import java.util.Map;
import java.util.HashSet;
public class TwoSum {
public static Set<Integer, Integer> twoSum(int targetSum, int input[]) {
Map<Integer, Integer> map = new HashMap<>();
Set<Integer, Integer> results = new HashSet<>();
for(int i=0; i< input.length; i++){
if(map.containsKey(input[i])) {
results.add(input[i], targetSum-input[i]);
}
else {
map.put(sum-input[i],i);
}
}
}
public static void main(String args[]){
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment