➜ ~

Playing Hacks and Stuffs!


Project maintained by h4ckyou Hosted on GitHub Pages — Theme by mattgraham

Move Zeroes

image

We have an integer array that may or may not have some zeroes.

Our goal is to move all the zeroes to the right of the array without disturbing the original order of all the remaining elements. We desire a space complexity of O(1) since we’re to solve this in-place meaning it won’t use additional memory space

Thinking about the problem a little more, we do not have to do anything with the zeroes as long as we maintain the relative order of the other elements.

This signals firstly that we do not need any extra space to store the zero’s

A very simple approach will be:

This method will work in-place without taking any extra space.

Solve Script: link image