You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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]
Solution:
using pointer from starting zero if any elemnt in nums array not equal to 0 then we add that element in starting and incrementing the pointer so thta all non zero elements come to starting and if increment != length up to length add zeros to length
//sudo code
int point = 0;
for(int num :nums){
if(num! = 0){
nums[point++] = num;
}}
while(point <nums.length){
nums[point++] =0;
}
by this way we can add all zeroes at last and non zeroes at first