Move Zeroes Problem & Solution

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.

See the move zeroes problem on LeetCode.

C++ Solution

#pragma GCC optimize("Ofast")
#pragma GCC optimization("unroll-loops")

static const int _=[](){std::ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);return 0;}();

class Solution {
public:
  void moveZeroes(vector<int>& nums) {
    for (int i = 0; i < nums.size(); ++i) {
      if (nums[i] == 0) {
        int j = i + 1;

        // Skips consecutive zeroes.
        while (j < nums.size() && nums[j] == 0) {
          ++j;
        }

        rotate(nums.begin() + i, nums.begin() + j, nums.end());
      }
    }
  }
};

Start Here

Many paths, there are. Follow yours, you must.