Skip to content

Commit

Permalink
issue rathoresrikant#570 solved Move all zeroes to the end of an array
Browse files Browse the repository at this point in the history
This pull request implements a function that moves all zeroes in a given list to the end while maintaining the order of non-zero elements. The function iterates through the list, shifting non-zero elements forward and filling the remaining positions with zeroes, modifying the input list in place.
  • Loading branch information
Mugunth140 authored Oct 19, 2024
1 parent e2a69e2 commit 2ebdee7
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Algorithms/Move-Zeros-to-end/move_zeroes_to_end.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
def move_zeroes_to_end(arr):
# Initialize a pointer for the position of non-zero elements
non_zero_index = 0

# Iterate through the array
for i in range(len(arr)):
if arr[i] != 0:
# Move the non-zero element to the front
arr[non_zero_index] = arr[i]
non_zero_index += 1

# Fill the remaining positions with zeroes
for i in range(non_zero_index, len(arr)):
arr[i] = 0

# Example usage
array = [0, 1, 0, 3, 12]
print("Original array:", array)
move_zeroes_to_end(array)
print("Array after moving zeroes:", array)

0 comments on commit 2ebdee7

Please sign in to comment.