Skip to content

Instantly share code, notes, and snippets.

@st0le
Created June 29, 2013 23:37
Show Gist options
  • Save st0le/5893143 to your computer and use it in GitHub Desktop.
Save st0le/5893143 to your computer and use it in GitHub Desktop.
O(nlogn) solution to 2SUM
// Complexity - O(nlogn)
private int[] findPair_2(int[] A) {
Arrays.sort(A); // O(nlogn)
for (int i = 0, l = A.length; i < l; i++) { //O(nlogn)
int j = Arrays.binarySearch(A, i + 1, l, -A[i]);
if (j > i) return new int[]{A[i], A[j]};
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment