I need to move all 0's in an array to the end of the array.
Example: [1, 10, 0, 5, 7] should result in [1, 10, 5, 7, 0].
I am open to doing a reverse loop or a regular loop.
I cannot create a new array.
Here is what I have so far:
for (int i = arr.length; i <= 0; --i) {
if (arr[i] != 0) {
arr[i] = arr.length - 1;
}
}
Thanks!
This is my code with 2 for loops:
output: [4, 2, 1, 1, 5, 9, 0, 0, 0, 0, 0]
In case if question adds following condition.
Then following implementation will work fine.
Steps to be followed :