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 !! :)
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 !! :)
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