Skip to content

Instantly share code, notes, and snippets.

@RichardCSantana-zz
Created April 17, 2017 22:07
Show Gist options
  • Save RichardCSantana-zz/5465317cd2ae070d7a2c16def583e721 to your computer and use it in GitHub Desktop.
Save RichardCSantana-zz/5465317cd2ae070d7a2c16def583e721 to your computer and use it in GitHub Desktop.
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/**
* @author richard.santana
*/
public class SumTwo {
public static void main(String[] args) {
Integer[] input = new Integer[]{11,2,7,15};
Integer target = 9;
System.out.println(Arrays.toString(verify(input, target)));
}
private static Integer[] verify(final Integer[] input,final Integer target) {
Map<Integer, Integer> map = new HashMap<>();
for(int i =0; i < input.length;i++){
Integer value = map.get(input[i]);
if(value != null){
return new Integer[] {value,i};
}else{
Integer missing = target - input[i];
map.put(missing,i);
}
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment