Last active
April 15, 2022 13:16
-
-
Save venuduggireddy/90f9565bc09bbe74b9cbad85b6b5476e to your computer and use it in GitHub Desktop.
Move Zeros to the Left
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''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