PROBLEM STATEMENT:
Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements.
MY APPROACH:
1.I notice the pattern that all the non-zero numbers should stay in the same order and all zeros should go to the end for that I point k for non-zero elements that should be in stable order
2.I start with a k = 0 to mark the position for the next non-zero number.
3.Then I go to the array from start to end. Every time I find a non-zero number, I put it at index k and then move k to right which stays the non-zero elements in stable order.
4.After moving all non-zero numbers to the left side of array then the all remaining elements are must gonna be zeros so I fill the rest of the array from index k to the end with zeros.
METHODS USED:
Single Traversal
In-Place Modification

Top comments (0)