2 Sum All Pair I
public List<List<Integer>> allPairs(int[] array, int target){
List<List<Integer>> result = new ArrayList<List<Integer>>();
Map<Integer, List<Integer>> map = new HashSet<Integer, List<Integer>>();
for (int i = 0; i < array.length; i++){
//get other half of pair
List<Integer>indices = map.get(target - array[i]);
if (indices != null){
for (int j: indices){
results.add(Arrays.asList(j, i); //add pair to result
{
}
if (!map.containsKey(array[i])){ // if value at current index not in map
map.put(array[i], new ArrayList<Integer>()); // make new slot to store indices for current value
}
map.get(array[i]).add(i); // add current index
}
return result;
}Last updated