0

I have a problem where i have to find missing numbers within an array and add them to a set.

The question goes like so:

Array of size (n-m) with numbers from 1..n with m of them missing.

Find one all of the missing numbers in O(log). Array is sorted. Example:

n = 8

arr = [1,2,4,5,6,8]

m=2

Result has to be a set {3, 7}.

This is my solution so far and wanted to know how i can calculate the big o of a solution. Also most solution I have seen uses the divide and conquer approach. How do i calculate the big oh of my algorithm below ?

ps If i don't meet the requirement, Is there any way I can do this without having to do it recursively ? I am really not a fan of recursion, I simply cant get my head around it ! :(

var arr = [1,2,4,5,6,8];
var mySet = [];

findMissingNumbers(arr);

function findMissingNumbers(arr){
    var temp = 0;
    for (number in arr){                    //O(n)
        temp = parseInt(number)+1;
        if(arr[temp] - arr[number] > 1){
            addToSet(arr[number], arr[temp]);
        }
    }
}

function addToSet(min, max){

    while (min != max-1){
        mySet.push(++min);
    }
}
ipalibowhyte
  • 1,553
  • 2
  • 22
  • 34
  • I don't follow how this algorithm should do what it supposed to. Moreover, I am not sure how it does not crash, as it seems to me - the array is of size 6, and `arr[number]` in `if(arr[temp] - arr[number] > 1){` should crash when `number ==8`, for example. – amit Dec 18 '14 at 14:55
  • Also, is the input array always sorted? – amit Dec 18 '14 at 15:01
  • Yes it is always sorted plus I would add a condition for array out of bounds cheers – ipalibowhyte Dec 18 '14 at 15:03
  • possible duplicate of [Big O, how do you calculate/approximate it?](http://stackoverflow.com/questions/3255/big-o-how-do-you-calculate-approximate-it) – Pradhan Dec 18 '14 at 23:27

4 Answers4

2

There are two things you want to look at, one you have pointed out: how many times do you iterate the loop "for (number in arr)"? If you array contains n-m elements, then this loop should be iterated n-m times. Then look at each operation you do inside the loop and try to figure out a worst-case scenario (or typical) scenario for each. The temp=... line should be a constant cost (say 1 unit per loop), the conditional is constant cost (say 1 unit per loop) and then there is the addToSet. The addToset is more difficult to analyze because it isn't called every time, and it may vary in how expensive it is each time called. So perhaps what you want to think is that for each of the m missing elements, the addToSet is going to perform 1 operation... a total of m operations (which you don't know when they will occur, but all m must occur at some point). Then add up all of your costs.

n-m loops iterations, in each one you do 2 operations total of 2(n-m) then add in the m operations done by addToSet, for a total of something like 2n-m ~ 2n (assuming that m is small compared to n). This could be O(n-m) or also O(n) (If it is O(n-m) it is also O(n) since n >= n-m.) Hope this helps.

TravisJ
  • 1,592
  • 1
  • 21
  • 37
  • I implicitly assumed here that adding an element to the set had constant cost (inside the addToSet function). – TravisJ Dec 18 '14 at 15:02
1

In your code you have a complexity of O(n) in time because you check n index of your array. A faster way to do this is something like that :

  • Go to the half of your array
  • Is this number at the right place (this means the other ones will be too because array is sorted)
  • If it's the expected number : go to the half of the second half
  • If not : add this number in the set and go to the half of the first half
  • Stop when the number you looking at is at index size-1

Note that you can have some optimization, for example you can directly check if the array have the correct size and return an empty array. It depends of your problem.

My algorithm is also in O(n) because you always take the worst set of data. In my case I would be that we miss one data at the end of the array. So technically it should be O(n-1) but constants are negligible in front of n (assumed to be very high). That's why you have to keep in mind the average complexity too.

0xmax
  • 503
  • 1
  • 6
  • 26
  • You do realize this problem cannot be solved in `O(n)`, because the output size itself for array such as `[1,100000]` is itself not in `O(n)`? – amit Dec 18 '14 at 15:07
  • I don't follow you, can precise please ? – 0xmax Dec 18 '14 at 15:11
  • The problem is `O((MAX-MIN) + n)` - because the output itself is of size `MAX-MIN`, which becomes more significant then `n` for a very sparse array. For [1,100], the output size is 99, for exmaple, but without changing `n`, you can increase the output size as much as you'd like [1,10000....0] - and the run time of the algorithm will increase, because you have more numbers to generate for the output. – amit Dec 18 '14 at 15:16
  • @amit According to the problem description, `n` is the size of the input `(n-m)` plus the size of the output (`m`). – interjay Dec 18 '14 at 15:18
  • @interjay Got you now. I revert my comments. I refered to the traditional `n` as the size of the input. – amit Dec 18 '14 at 15:21
0

For what it's worth here is a more succinct implementation of the algorithm (javascript):

var N = 10;
var arr = [2,9];
var mySet = [];
var index = 0;
for(var i=1;i<=N;i++){
    if(i!=arr[index]){
        mySet.push(i);
    }else{
        index++;
    }
}

Here the big(O) is trivial as there is only a single loop which runs exactly N times with constant cost operations each iteration.

ReallyMadeMeThink
  • 1,061
  • 7
  • 11
-2

Big O is the complexity of the algorithm. It is a function for the number of steps it takes your program to come up with a solution.

This gives a pretty good explanation of how it works:

Big O, how do you calculate/approximate it?

Community
  • 1
  • 1
meyer9
  • 1,120
  • 9
  • 26
  • 2
    This does not answer the question, it is a very generic answer that doesn't address any aspect of the question (except the generic "calculate big O") – amit Dec 18 '14 at 14:50
  • I think this answer gives a good insight into what Big O is and how to calculate it. – meyer9 Dec 18 '14 at 14:52
  • But the question was not "What big O is and how to calculate it?" It was `How do i calculate the big oh of my algorithm below ?` – amit Dec 18 '14 at 14:53
  • And I provided a link on how to calculate the Big O. – meyer9 Dec 18 '14 at 14:55
  • Yes, but the question was not "How to calculate a big O" in general - it was how to calculate it here. A link is fine, as long as you give some more information on how to calculate it, and guidelines how to apply the linked theory to the SPECIFIC problem at hand – amit Dec 18 '14 at 14:57