Move Zeroes Leetcode Solution

Move Zeroes Leetcode Problem :

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.

Move Zeroes Leetcode Solution :

Constraints :

• 1 <= nums.length <= 104
• -231 <= nums[i] <= 231 – 1

Example 1:

• Input: nums = [0]
• Output: [0]

Approach :

This code is for a method called moveZeroes that takes an array of integers called nums as input and moves all the zeros to the end of the array while keeping the order of the non-zero elements unchanged. Let’s break down the code step by step:

1. Initialize a variable i to 0. This variable will be used to keep track of the index where the next non-zero element should be placed.
2. Start a loop that iterates over each element num in the nums array.
3. Check if the current element num is not equal to 0 (which means it’s a non-zero element).
4. If the current element is non-zero, assign it to the position nums[i] in the array, and then increment i by 1. This moves the non-zero element to the left side of the array.
5. After processing all the elements in the array, there might be some remaining positions towards the end of the array that need to be filled with zeros.
6. Start another loop that continues from the last value of i until the end of the array (nums.length).
7. Set the value of nums[i] to 0, effectively placing zeros in the remaining positions at the end of the array.
8. Increment i by 1 in each iteration to move to the next position in the array.
9. Once this loop is completed, all the non-zero elements have been moved to the front of the array, and any remaining positions towards the end of the array have been filled with zeros.

The code rearranges the array in-place, meaning it modifies the original nums array without creating a new one. After calling this moveZeroes method, the array nums will have all the zeros moved to the end while preserving the order of the non-zero elements.

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others