do you realy need the final array with the negatives filled in?...when they can be inferred in any further access to the array, ie the negative element won't be there. also, since this looks like a binary problem (the result is either 1 or 0) you should realy think about expressing it as such, and save LOTS of space and time walking through it.