Compact a given array problem
Posted
by Bragaadeesh
on Stack Overflow
See other posts from Stack Overflow
or by Bragaadeesh
Published on 2010-04-30T05:45:31Z
Indexed on
2010/04/30
5:47 UTC
Read the original article
Hit count: 353
problem-solving
|arrays
Dont know whether this is a duplicate, but this was an interview question asked to me. Given an array of random numbers and -1 placed inbetween, I have to compact the array meaning all the -1s are to be replaced and the final output should be the last valid index with the fresh array. For example.
Input:
3 4 -1 -1 -1 5 8 -1 8
Output:
3 4 5 8 8 5 8 -1 8 and last valid index is 4
Input:
-1 -1 -1 -1 -1 2
Output:
2 -1 -1 -1 -1 2 and last valid index is 0
Input:
-1 -1 -1 3 3 3
Output:
3 3 3 3 3 3 and last valid index is 2
You should not swap the values just the last valid index along with the array is enough to decipher the non negative values.
© Stack Overflow or respective owner