Given an integer array nums, move all 0’s to the end of it while maintaining the relative order of the non-zero elements.
Note that you must do this in-place without making a copy of the array.
?
Input: nums = [0,1,0,3,12]
Output: [1,3,12,0,0]
Input: nums = [0]
Output: [0]
From: LeetCode
Link: 283. Move Zeroes
void moveZeroes(int* nums, int numsSize) {
int insertPos = 0; // This will keep track of the position where we need to insert the next non-zero element.
for (int i = 0; i < numsSize; i++) {
if (nums[i] != 0) {
nums[insertPos++] = nums[i]; // Move non-zero elements to the front and increment insertPos.
}
}
while (insertPos < numsSize) {
nums[insertPos++] = 0; // Fill the remaining array with zeroes.
}
}