Array contains 0 and positive numbers.
Approach 1: Brute force, iterate over array if element is 0, find the next non-zero element to swap, using while loop.
Good, solves the issue but failed performance test cases. As nested loops are used.
Time Complexity: O(n*m), where m is number of elements to the right of current element.
Space Complexity: O(1), no memory resource utilized.
Approach 2; To obtain result in single loop, create a new array vector the first push the non-zero elements then remaining zeros.
Passed all the test cases
Time Complexity: O(n)
Space Complexity: O(n)
.
Top comments (0)