Move Zeroes
To solve this coding challenge, we need to move all zeroes in an integer array to the end while maintaining the relative order of the non-zero elements. This needs to be done in-place without creating a copy of the array.
Explanation
The task is to move all zeros in the given array to the end while maintaining the relative order of non-zero elements. To do this in-place, we need to take several steps:- Traverse the Array:
- Traverse the given array and count how many zeros are there.
- While counting zeros, we need to reposition non-zero elements to fill the positions previously occupied by zeros.
- Shift Non-zero Elements:
- Whenever we encounter a non-zero element, we move it to the position adjusted by the number of zeros encountered so far.
- This keeps non-zero elements in their original order but places them at the earliest available position.
- Fill Remaining with Zeros:
- After repositioning non-zero elements, the positions towards the end of the array, left vacant by previous zeros, should be filled with zeros.
- Initialize Variables:
-
Initialize a variable
to count the number of zeros encountered.
zero_count -
Determine the length of the array (
).
n - First Pass - Shift Non-zero Elements:
- Iterate over the array using a loop.
- For each element:
-
If it is zero, increment
.
zero_count -
If it is non-zero, shift it to the left by the number of zeros encountered using the formula
.
nums[i-zero_count] = nums[i] - Second Pass - Fill Remaining with Zeros:
-
After the first pass, start another loop from
to
n - zero_count.n - For each position, set the element to zero.
-
In the first step, we initialize
to keep track of how many zeros we have encountered so far. We also determine the length of the input array (
zero_count).n -
Next, we iterate over each element in the array from the beginning to the end. If we encounter a zero, we increment the
. This helps us keep track of how many zeros we have seen so far.
zero_count -
As we encounter non-zero elements, we move them to an earlier position in the array which is determined by subtracting
from the current index
zero_count. This effectively skips over the positions that were initially occupied by zeros.i -
Finally, after repositioning all non-zero elements, the trailing elements in the array still remain at their original positions. We then loop from the position
to the end of the array and fill these positions with zeros.
n - zero_count
Detailed Steps in Pseudocode
Pseudocode
Pseudocode: Initializing Variables and First Pass
# Initialize variable to count zeros encountered
zero_count = 0
# Get the length of the array
n = length of nums
# First pass: Shift non-zero elements to the left
for i from 0 to n-1:
if nums[i] == 0:
# If the element is zero, increment zero_count
zero_count += 1
else:
# If the element is non-zero, move it to the correct position
nums[i - zero_count] = nums[i]
Pseudocode: Second Pass - Fill Remaining with Zeros
# Second pass: Fill the end of the array with zeros
for i from (n - zero_count) to n-1:
# Fill the position with zero
nums[i] = 0
Step-by-Step Explanation:
zero_count = 0
n = length of nums
for i from 0 to n-1:
if nums[i] == 0:
zero_count += 1
else:
nums[i - zero_count] = nums[i]
for i from (n - zero_count) to n-1:
nums[i] = 0