Compact a given array problem -


dont know whether duplicate, interview question asked me. given array of random numbers , -1 placed inbetween, have compact array meaning -1s replaced , final output should last valid index fresh array. example.

input: 3 4 -1 -1 -1 5 8 -1 8 output: 3 4 5 8 8 5 8 -1 8 , last valid index 4  input: -1 -1 -1 -1 -1 2 output: 2 -1 -1 -1 -1 2 , last valid index 0  input: -1 -1 -1 3 3 3 output: 3 3 3 3 3 3 , last valid index 2 

you should not swap values last valid index along array enough decipher not -1 values.

int k=0 i=0; i<len; i++   if arr[i]!=-1       arr[k]=arr[i]       k++   end if return k-1 

Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -