1 Jul 2019

Move all zeros to the end

Problem statement:
     Move all zeros towards the end in a given array.

Input : [0 1 2 0 3 0 0 4 0 0 0 5]

Soltion:

There are multiple approches to solve this problem, we will solve this problem using two pointers with
Space Complexity = O(1)
Time Complexity = O(n)

The idea here is to take two pointers one of which move at its speed and other one would count the number of non zero-elements.

Walk through the code for the above example.


Post your suggestions and comments.
Happy Coding !! :)


1 comment:

  1. Master the art of coding with efficient algorithms and clean code practices—optimize, refactor, and always strive for simplicity.emailtooltester A great recipe for success? Write once, debug twice, and never compromise on readability!








    ReplyDelete