Skip to content

Instantly share code, notes, and snippets.

@Josephchinedu
Created May 11, 2022 07:58
Show Gist options
  • Save Josephchinedu/2a87c62d4b872355ac7de475ca95bb6d to your computer and use it in GitHub Desktop.
Save Josephchinedu/2a87c62d4b872355ac7de475ca95bb6d to your computer and use it in GitHub Desktop.
move zeroes
Given an array of integers, write a function to move all "0" to the end od the array or list while maintaing the
relative orde of the elements.
Explanation:
move the non zero elements to the beginning of the array and keep track of their count, initialize a variable for it call, e.g "j".
then, after that set the elements starting from "j" till the end of the array to "0".
Steps:
a. define a pointer "j" at the beginning of the array.
j = 0
b. assign the length of the array to a variable
n = len(nums)
c. loop over the array. for each of the element, check if the current element is not zero.
If that's the case, set the value of the array at the position "j" to be the current element.
Then increament "j"
d. after that, set the remaining value after "j" until the end of the array to be zero.
time complexity: O(n)
space complexity: 0(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment