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:
  1. 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.
  2. 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.
  3. 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.
    Here is the step-by-step pseudocode which explains the logic in great detail:

    Detailed Steps in Pseudocode

  4. Initialize Variables:
    • Initialize a variable
      zero_count
      to count the number of zeros encountered.
    • Determine the length of the array (
      n
      ).
  5. 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]
        .
  6. Second Pass - Fill Remaining with Zeros:
    • After the first pass, start another loop from
      n - zero_count
      to
      n
      .
    • For each position, set the element to zero.

    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:

  7. In the first step, we initialize
    zero_count
    to keep track of how many zeros we have encountered so far. We also determine the length of the input array (
    n
    ).
    •                                             
      zero_count = 0
      n = length of nums
      
                                              
  8. Next, we iterate over each element in the array from the beginning to the end. If we encounter a zero, we increment the
    zero_count
    . This helps us keep track of how many zeros we have seen so far.
    •                                             
      for i from 0 to n-1:
      if nums[i] == 0:
      zero_count += 1
      else:
      nums[i - zero_count] = nums[i]
      
                                              
  9. As we encounter non-zero elements, we move them to an earlier position in the array which is determined by subtracting
    zero_count
    from the current index
    i
    . This effectively skips over the positions that were initially occupied by zeros.
  10. 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
    n - zero_count
    to the end of the array and fill these positions with zeros.
    •                                             
      for i from (n - zero_count) to n-1:
      nums[i] = 0
      
                                              
By following these steps, the challenge is solved efficiently in-place with minimal additional operations.