283. Move Zeroes
Question
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.
Example 1:
Input: nums = [0,1,0,3,12]
Output: [1,3,12,0,0]
Example 2:
Input: nums = [0]
Output: [0]
Constraints:
1 <= nums.length <= 104-231 <= nums[i] <= 231 - 1
Follow up: Could you minimize the total number of operations done?
Approach 1: Brute Force
Algorithm
- Create a new vector for non zero value
nonZero - Iterate through each element in
numsusingforloop & push non zero elements innonZero - for loop till
nonZero.size()replay all the elements ofnumswithnonZero - Loop through rest of the
numsand replace with zeros
Code
class Solution {
public:
void moveZeroes(vector<int>& nums) {
vector<int> nonZeros;
int n = nums.size();
for (int i = 0 ; i < n ; i++) {
if (nums[i] != 0) nonZeros.push_back(nums[i]);
}
int n2 = nonZeros.size();
for (int i = 0 ; i < n2 ; i++) {
nums[i] = nonZeros[i];
}
for (int i = n2 ; i < n ; i++) {
nums[i] = 0;
}
return;
}
};
Complexity Analysis
- Time Complexity:
- 3 loops for worst case n elements
- Space Complexity:
- new array (vector) for non-zeros worst case n elements
Approach 2: Better
Algorithm
- Iterate through each element in the list (
nums) except for the last one. - Check if the current element is zero:
- If it is, start looking ahead (from the next position) to find the first non-zero element.
- Find the nearest non-zero element:
- Start at the next position (
j = i + 1) and move forward until either:- You find a non-zero element.
- You reach the end of the list.
- Start at the next position (
- Swap the zero with the non-zero element:
- Once a non-zero element is found, swap it with the zero found at index
i.
- Once a non-zero element is found, swap it with the zero found at index
- Repeat this process for each element in the list until all zeros are moved to the end, maintaining the order of the non-zero elements.
Code
class Solution {
public:
void moveZeroes(vector<int>& nums) {
for (int i = 0; i < nums.size() - 1; i++) {
if (nums[i] == 0) {
int j = i + 1;
while (j < nums.size() && nums[j] == 0) {
j++;
}
if (j < nums.size()) {
swap(nums[i], nums[j]);
}
}
}
}
};
Complexity Analysis
- Time Complexity:
- Space Complexity:
Approach 3: Optimal
Algorithm
- Initialize a
lastNonZeroFountAtint with0 - Loop through the
nums- Replace the
nums[nonZeroIndex]with non-zero elements - Increment the
nonZeroIndexafter every replacement only
- Replace the
- From the last
nonZeroIndexto the end of thenumsvector replace the elements with0
Code
class Solution {
public:
void moveZeroes(vector<int>& nums) {
int nonZeroIndex = 0;
// Move all non-zero elements to the front
for (int i = 0; i < nums.size(); i++) {
if (nums[i] != 0) {
nums[nonZeroIndex++] = nums[i];
}
}
// Fill the remaining elements with zeroes
for (int i = nonZeroIndex; i < nums.size(); i++) {
nums[i] = 0;
}
}
Complexity Analysis
- Time Complexity:
- Space Complexity: