Skip to content

Instantly share code, notes, and snippets.

@hkasera
Created June 1, 2020 23:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hkasera/42fb2590a50968d5fd3c86e3a4b8e729 to your computer and use it in GitHub Desktop.
Save hkasera/42fb2590a50968d5fd3c86e3a4b8e729 to your computer and use it in GitHub Desktop.
private boolean sumsToTarget(int[] arr, int k) {
Arrays.sort(arr);
int lhs = 0, rhs = arr.length - 1;
while (lhs < rhs) {
int sum = arr[lhs] + arr[rhs];
if (sum == k)
return true;
else if (sum < k)
lhs++;
else rhs--;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment