Since the numbers in array are starting from 1 we can get the sum of all elements in array and subtract it from expected sum of elements if there wasn’t missing any. That should give us the element missing.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// Result: 100/100 public static int solution_3_PermMissingElem(int[] A) { if (A.length == 0) return 1; int sumOfAllNumbers = 0; for (int num : A) sumOfAllNumbers += num; long N = A.length; long expectedSumOfAllNumbers = ((N + 1)*(N + 2))/2; long missingNumber = expectedSumOfAllNumbers - sumOfAllNumbers; return (int) missingNumber; } |