Skip to content

Instantly share code, notes, and snippets.

@facekunal
Last active July 11, 2020 09:43
Show Gist options
  • Save facekunal/62fe2f1b9d3c8e2382dba65361ef0afe to your computer and use it in GitHub Desktop.
Save facekunal/62fe2f1b9d3c8e2382dba65361ef0afe to your computer and use it in GitHub Desktop.
boolean isEqualSum(int[] a, int[] b) {
Arrays.sort(a);
Arrays.sort(b);
int sumA = Arrays.stream(a).sum();
int sumB = Arrays.stream(b).sum();
int diff = sumB - sumA;
if(diff % 2 == 1) {
return false;
} else {
diff = diff / 2;
}
int j = 0;
for(int i=0;i<a.length;i++) {
int currdiff = b[j] - a[i];
if(diff == currdiff) {
return true;
} else if(currdiff < diff) {
j++;
} else if(currdiff > diff) {
continue;
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment