Skip to content

Instantly share code, notes, and snippets.

@tiagopereira17
Last active July 19, 2023 12:43
Show Gist options
  • Save tiagopereira17/c9172b67fed3ad5db8419d8929a6e435 to your computer and use it in GitHub Desktop.
Save tiagopereira17/c9172b67fed3ad5db8419d8929a6e435 to your computer and use it in GitHub Desktop.
Find the missing element in a given permutation.
public class PermMissingElem {
public int findMissingElement(int[] A) {
if(A == null || A.length == 0) {
return 1;
}
long N = A.length + 1;
long total = N * (N + 1) / 2;
long sum = 0L;
for(int i = 0; i < A.length; i++) {
sum += A[i];
}
return (int) (total - sum);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment