Skip to content

Instantly share code, notes, and snippets.

@venuduggireddy
Last active April 15, 2022 13:16
Show Gist options
  • Save venuduggireddy/90f9565bc09bbe74b9cbad85b6b5476e to your computer and use it in GitHub Desktop.
Save venuduggireddy/90f9565bc09bbe74b9cbad85b6b5476e to your computer and use it in GitHub Desktop.
Move Zeros to the Left
'''Problem Statement
Given an integer array, move all elements that are 0 to the left while maintaining the order of other elements in the array.
The array has to be modified in-place.'''
def move_zeros_to_left(A):
for i, ele in enumerate(A, start=0):
if(ele == 0):
A.pop(i)
A.insert(0, 0)
return A
v = [1, 10, 20, 0, 59, 63, 0, 88, 0]
print("Original Array:", v)
move_zeros_to_left(v)
print("After Moving Zeroes to Left: ", v)
#Original Array
#1, 10, 20, 0, 59, 63, 0, 88, 0,
#After Moving Zeroes to Left
#0, 0, 0, 1, 10, 20, 59, 63, 88,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment