you have an array and some 0 in that array. memory is so low that you can not create another array. how do you remove 0 from the array? how do you optimize it.
Anonymous
Agreed that this is an odd question, but just to answer it: Iterate from the front of the array and if you detect an element to delete (in this case 0) then swap with the last item in the array that is a valid number (not 0). So just have two pointers, one pointer checking all the values at the start of the array for validity, and a pointer in the back of the array to swap with the next valid number should a 0 exist in the front of the array. terminate when the backPointer <= frontPointer. Return the array and the frontPointer index.
Check out your Company Bowl for anonymous work chats.