Last active
July 11, 2020 09:43
-
-
Save facekunal/62fe2f1b9d3c8e2382dba65361ef0afe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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