Last active
July 19, 2023 12:43
-
-
Save tiagopereira17/c9172b67fed3ad5db8419d8929a6e435 to your computer and use it in GitHub Desktop.
Find the missing element in a given permutation.
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
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