Skip to content

Instantly share code, notes, and snippets.

@alexandervasyuk
Created October 1, 2014 23:59
Show Gist options
  • Save alexandervasyuk/e9d6205baac49d1ce0fc to your computer and use it in GitHub Desktop.
Save alexandervasyuk/e9d6205baac49d1ce0fc to your computer and use it in GitHub Desktop.
pairsThatSumUpToK Optimized
public static List<Pair> pairsThatSumUpToK(int[] a, int k) {
HashSet<Integer> set = new HashSet<Integer>();
List<Pair> result = new ArrayList<Pair>();
for (int i = 0 ; i < a.length; i++) {
int lookup = k - i;
if (set.contains(lookup)) {
result.add(new Pair(lookup, i));
} else {
set.add(i);
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment