Python Program to Move all the zeros to the end of Array29 Aug 2024 | 3 min read In this tutorial, we will write the Python program to move all the zero at the end of the array. The problem statement is a given array of the random number consists of some zero at the random place at zero, but we need to maintain the order of the given array. For example - If the given array is [2, 7, 6, 0, 9, 0, 1, 0, 0, 8, 0], it should be transform to [2, 7, 6. 9, 1, 8, 0, 0, 0, 0, 0]. The array has maintained the relative order of items in the array. The expected time complexity is O(n) and extra space O(1). Example - There are several ways to solve this problem, and we will use some methods to solve this excitingly. Solution ImplementationIn this method, we traverse the given array left to right and place the element at the next available position in the array. Once all the non-zero elements are placed, we fill all the remaining indices with zeros. Let's implement this approach through the Python program. Example - Output: [8, 7, 4, 2, 1, 0, 0, 0] Explanation - In the above code, we defined the move_zero() function that takes a list as an argument. Inside the function, we initialized the variable with zero, which keeps track of the next available index position. Then, we traverse the list and check whether a current element is non-zero; if true, then put it at the next position and increase the counter of k. Once all the non-zero elements are stored in the list, we run another for loop from k to the given list's length. At the remaining indices, we put the zero at the end of the list. Time Complexity - The time complexity is 0(n), where n is the size of the input. The above solution is easy to implement. Let's understand another solution. Method - 2: Using the partitioning logic of QuicksortWe can also solve this problem by the partitioning of the Quicksort's portioning logic. In this approach, we make a 0 as a pivot and make one pass of the partitioning process. The partitioning logic reads all elements and swaps every non-pivot element with the first occurrence of the pivot. Let's understand the following example. Example - Output: [6, 8, 2, 3, 4, 1, 0, 0, 0] Explanation - In the above code, we created a swap function to swap the elements. Then, we created the partition() method, which calls the swap method and each time we encounter a non-zero ''j'' is increased. The element is placed before the pivot. The time complexity of the above code is 0(n), where n is the size of the input. ConclusionIn this tutorial, we have discussed some solutions to the above problem. It is an introductory Python list-related problem, and this problem can be asked in the technical interviews. |
What is HTTP? HTTP stands for HyperText Transfer Protocol which has some set of rules which determines the communication or data transfer into client-server architecture. The client is generally a browser, and the Server is the source where information is available already, and the Client requests the...
3 min read
Introduction: In this article, we are discussing how to suppress warnings in Python. If you code in Python and are a programmer, you must be faced with a warning at any time. A warning in Python mainly displays a message on the screen. The message does not...
6 min read
In this article, we will discuss how we can input a list in Python. But before discussion their methods, we must know about the list in Python. What is a List? A list is a built-in data structure provided by Python, which enables the organization and storage of...
6 min read
What is Edge Computing? To move the burden closer to the location where data is produced and to enable actions to be performed in response to an evaluation of that data, edge computing technologies make use of computer power that is accessible outside of conventional and cloud...
13 min read
Variables in Python language serve as the storage units for the various data values and data structures. When a variable is assigned to any Python object, it then points toward that object since they are reference, or a pointer, to that particular object in the memory...
7 min read
Metaprogramming might sound new, but if the user has ever worked with Decorators or metaclasses, they have used metaprogramming in their projects. Therefore, we can say that metaprogramming is the program that is used for manipulating the program. In this tutorial, we will discuss the metaclasses and...
9 min read
Pip is a package management system used to install and manage software packages written in Python. It stands for "Pip Installs Packages" and it allows us to easily download, upgrade, and manage libraries and dependencies used in our Python projects. Using pip, we can install packages from...
6 min read
Stacks are linear data structures that follow the Last-In-First-Out (LIFO) principle, which states that the item that was most recently added is the one that gets deleted first. A stack's fundamental commands are "push", "pop", "peek" (or top), and "isEmpty". Each stack element in a linked...
5 min read
In the Python programming language, we have the concept of dictionaries. The dictionaries are mutable, and we can easily add and delete the items from the dictionary. It is a collection of unordered data items. A dictionary is consists of two parts and the first is the...
9 min read
In this tutorial, we will write a Python program to find the difference between the two given strings. This problem can be asked in the interview. Let's understand the problem statement and then we will approach to the solution. Problem Statement - There are two strings given s...
3 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India