Interview Question
Country: United States
what do you mean missing elements in array? maybe, missing elements in arithmetic progression?
This post discusses 2 solutions: Sum and Xor.
capacode. com/array/find-missing-number-in-array/
Dude I'm not asking there is one number in the range from 0 to N missing.
I'm asking more than one element missing from 0 to n .
Please understand :-( :-(
hi, sorry, my bad! I didn't read it carefully.
Anyway, here is my thinking.
If you have O(n) space available, then do a simple counting, then check for entries with zero count:
init Count[i] = 0 for all i.
for (i=0..size(A)-1) Count[A[i]]++
for (i=0..n-1) if Count[i] ==0: print i
If you are restricted to use only a constant extra memory (except the array itself), then you need to sort the array in-place. Unfortunately, I don't see any in-place sorting algorithm that runs in linear time. So, probably quick-sort is good choice.
After sorting in-place, just check pairs of adjacent elements in array, if they differ more than 1, the numbers between them are missing.
I doubt that O(n) time O(1) space algorithm exists.
So, either O(n) time O(n) space algorithm, or O(1) space O(nlogn) time algorithm exists.
Tell me if I am wrong.
well, why not just substract the current int with the previous one (ie. n - (n-1))? if the diff is more than 1, there is one missing in between.
ok in theory if you know a1+a2+..ak, a1^ a1 + a2^a2 + a3^a3 +a4^a4+...ak^ak,.....
a1^k+....ak^k, you will get a1,a2....ak when you solve this equation in some way.
a1+....ak is n*(n+1)/2 - sum_of_array, a1^ a1 + a2^a2 + a3^a3 +a4^a4+...ak^ak is n*(n+1)*(2n+1)/6 - sum of cubes of array elements... sum of powers - there is a formule which convert to polinom of n - sum of k-th degree of array items
I personally prefer to avoid such calculation and to sort array with O(n) complexity.
It could be solved by counting sort:
1. find the min and max of the elements in the array
2. create any array of size max and init each element to 0
3. use counting sort to get the occurrence of elements in the array
4. check the new array from position min to max, the position of zero are the missing numbers
I could not find a way to resolve this question with the info provided without sorting the array/list first, and then finding the missing values from 1 - k-1 (assume MergeSort O(nLogn) followed by a scan of O(n)
Simply put, you cannot have an array of size K and the elements are 1-K-1
Example: K=5
Array {3,2,1,4, ??? } <-- It can't be 0 (>1), and it can't be 5 (<= k-1), and size of array = 4 not 5
Also, let's say that the OP did not write the question properly, and K is not the size of the array but the max value in the array
For example: {3,2,5,7}
Then K = 7 and the missing values are {1,4,6}. In this case, the array has to be scanned to get K, then processed to find missing values. 2*O(n)
Or let's say K is provided as input, then a valid input with K = 100 is array = {1}...
98 missing numbers.
Regardless, here's my solution that runs in 2*O(n). The assumptions I made:
1. K is provided is part of the input.
2. Missing elements are only one-apart from existing elements. For example
K = 13
Input is { 1,8,3,5,7,6,2,11,12}
Output would be {9,4,10}
I didn't test for edge cases by the way...
Code
And the driver code
- fayezelfar January 19, 2016