Array Question 7

Home / Java Practice Question / Array Question 7

Array Question 7


Question 7. Write a Program to move all zeros 0 to the end of the array while maintaining the relative order of the non-zero elements.

Program- 


class Solution {

    public void moveZeroes(int[] nums) {

        int nz=0;

        int z=0;

        while(nz<nums.length){

            if(nums[nz]!=0){

                int temp=nums[z];

                nums[z]=nums[nz];

                nums[nz]=temp;

                nz++;

                z++;

            }

            else{

                nz++;

            }

        }  

    }

}