Move Zeros to End in JavaAn array of unsorted integers is given. Our task is to move all the zero elements to the end and nonzero elements to the front. Note the relative arrangement of the nonzero elements should never get disturbed. The following examples make things clearer. Example 1: Input: int arr[] = {6, 7, 0, 2, 1, 78, 0, 56, 0, 4} Output: arr[] = {6, 7, 2, 1, 78, 56, 4, 0, 0, 0} Explanation: While maintaining the relative order of the nonzero elements, it is the only solution possible. Note that the relative order means if the position of one element is before the other element, then that order has to be maintained in the output. In the input array, 78 comes before 56; therefore, in the output also, 78 comes before 56. The same logic can be applied to the other nonzero elements of the array. Example 2: Input: int arr[] = {8, 9, 2, 3, 0, 0, 0} Output: arr[] = {8, 9, 2, 3, 0, 0, 0} Explanation: Here, in the input array, the zeros are already there at the end. Hence, without any modification, we can return the input array. Approach: Using Auxiliary ArrayThe approach is simple. Observe the following steps. Step 1: Create an auxiliary array of the same size as the input array. Step 2: Create a variable zeroCount, and initialize it with zero. Also, create a variable outputIndex, initializing with the value 0. Step 3: Using a loop, iterate over the elements of the input array. If an element with the zero value is encountered, increment the zeroCount by 1. If an element with a nonzero value is encountered, copy the value of that element in the element auxiliarArray[outputIndex], and increment the value of outputIndex by 1. Step 4: Add 0 to the end of the auxiliary array. The number of zero should be equal to the value contained in the variable zeroCount. It can be done with the help of loop. Step 5: Display the output array on the console. ImplementationObserve the implementation of the mentioned steps. FileName: MoveZeros.java Output: For the following array: 6 7 0 2 1 78 0 56 0 4 The answer after moving the zero at the end is: 6 7 2 1 78 56 4 0 0 0 For the following array: 8 9 2 3 0 0 0 The answer after moving the zero at the end is: 8 9 2 3 0 0 0 Complexity Analysis: Since the loop is used for iterating over the element and appending zero at the end in the method solve(), the time complexity of the program is O(N). Also, an auxiliary array is used for storing the result. Therefore, the space complexity of the program is O(N), where N is the total number of elements present in the input array. We will do the optimization in terms of the space complexity in the following approach. Approach: Using Two PointersWe will take twopointers. One pointer of the zerovalued elements and the other for the nonzero valued elements. The task is to put zerovalued elements towards the end. Therefore, the nonzero pointer points only when a nonzero element is encountered after the zeroelement pointer. Whenever we see that the nonzero and the zero pointers are set, the elements can be swapped that are located at these places in the input array. After that, we move the zeropointer further. We also move the nonzero pointer until a nonzero element is found. When the nonzero pointer reaches the end of the input array, we stop. The illustration of the same is mentioned below. ImplementationFileName: MoveZeros1.java Output: For the following array: 6 7 0 2 1 78 0 56 0 4 The answer after moving the zero at the end is: 6 7 2 1 78 56 4 0 0 0 For the following array: 8 9 2 3 0 0 0 The answer after moving the zero at the end is: 8 9 2 3 0 0 0 Complexity Analysis The time complexity of the program is the same as the previous program. Since no auxiliary space is used in the program, the space complexity of the program is constant, i.e., O(1).
Next TopicCount Inversions in an array in Java
