Dada una array de números aleatorios, Empuje todos los ceros de una array dada al final de la array. Por ejemplo, si las arrays dadas son {1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0}, debe cambiarse a {1, 9, 8, 4, 2, 7, 6, 0, 0, 0, 0}. El orden de todos los demás elementos debe ser el mismo. La complejidad de tiempo esperada es O(n) y el espacio extra es O(1).
Ejemplo:
Input : arr[] = {1, 2, 0, 4, 3, 0, 5, 0}; Output : arr[] = {1, 2, 4, 3, 5, 0, 0, 0}; Input : arr[] = {1, 2, 0, 0, 0, 3, 6}; Output : arr[] = {1, 2, 3, 6, 0, 0, 0};
C
// A C program to move all zeroes at the end of array #include <stdio.h> // Function which pushes all zeros to end of an array. void pushZerosToEnd(int arr[], int n) { int count = {0}; // Count of non-zero elements // Traverse the array. If element encountered is non- // zero, then replace the element at index 'count' // with this element for (int i = 0; i < n; i++) if (arr[i] != 0) arr[count++] = arr[i]; // here count is // incremented // Now all non-zero elements have been shifted to // front and 'count' is set as index of first 0. // Make all elements 0 from count to end. while (count < n) arr[count++] = 0; } // Driver program to test above function int main() { int arr[] = {1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0, 9}; int n = sizeof(arr) / sizeof(arr[0]); pushZerosToEnd(arr, n); printf("%s\n", "Array after pushing all zeros to end of array:"); for (int i = 0; i < n; i++) printf("%d ", arr[i]); return 0; }
C++
#include <algorithm> #include <iostream> #include <vector> void push_zeros_to_end(std::vector<int>& arr) { std::stable_partition(arr.begin(), arr.end(), [](int n) { return n != 0; }); } int main() { std::vector<int> arr{1,9,8,4,0,0,2,7,0,6,0,9}; push_zeros_to_end(arr); for(const auto& i : arr) std::cout << i << ' '; std::cout << "\n"; return 0; }
Java
/* Java program to push zeroes to back of array */ import java.io.*; class PushZero { // Function which pushes all zeros to end of an array. static void pushZerosToEnd(int arr[], int n) { int count = 0; // Count of non-zero elements // Traverse the array. If element encountered is // non-zero, then replace the element at index 'count' // with this element for (int i = 0; i < n; i++) if (arr[i] != 0) arr[count++] = arr[i]; // here count is // incremented // Now all non-zero elements have been shifted to // front and 'count' is set as index of first 0. // Make all elements 0 from count to end. while (count < n) arr[count++] = 0; } /*Driver function to check for above functions*/ public static void main (String[] args) { int arr[] = {1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0, 9}; int n = arr.length; pushZerosToEnd(arr, n); System.out.println("Array after pushing zeros to the back: "); for (int i=0; i<n; i++) System.out.print(arr[i]+" "); } } /* This code is contributed by Devesh Agrawal */
Python3
# Python3 code to move all zeroes # at the end of array # Function which pushes all # zeros to end of an array. def pushZerosToEnd(arr, n): count = 0 # Count of non-zero elements # Traverse the array. If element # encountered is non-zero, then # replace the element at index # 'count' with this element for i in range(n): if arr[i] != 0: # here count is incremented arr[count] = arr[i] count+=1 # Now all non-zero elements have been # shifted to front and 'count' is set # as index of first 0. Make all # elements 0 from count to end. while count < n: arr[count] = 0 count += 1 # Driver code arr = [1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0, 9] n = len(arr) pushZerosToEnd(arr, n) print("Array after pushing all zeros to end of array:") print(arr) # This code is contributed by "Abhishek Sharma 44"
C#
/* C# program to push zeroes to back of array */ using System; class PushZero { // Function which pushes all zeros // to end of an array. static void pushZerosToEnd(int []arr, int n) { // Count of non-zero elements int count = 0; // Traverse the array. If element encountered is // non-zero, then replace the element // at index â..countâ.. with this element for (int i = 0; i < n; i++) if (arr[i] != 0) // here count is incremented arr[count++] = arr[i]; // Now all non-zero elements have been shifted to // front and â..countâ.. is set as index of first 0. // Make all elements 0 from count to end. while (count < n) arr[count++] = 0; } // Driver function public static void Main () { int []arr = {1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0, 9}; int n = arr.Length; pushZerosToEnd(arr, n); Console.WriteLine("Array after pushing all zeros to the back: "); for (int i = 0; i < n; i++) Console.Write(arr[i] +" "); } } /* This code is contributed by Anant Agrawal */
PHP
<?php // A PHP program to move all // zeroes at the end of array // Function which pushes all // zeros to end of an array. function pushZerosToEnd(&$arr, $n) { // Count of non-zero elements $count = 0; // Traverse the array. If // element encountered is // non-zero, then replace // the element at index // 'count' with this element for ($i = 0; $i < $n; $i++) if ($arr[$i] != 0) // here count is incremented $arr[$count++] = $arr[$i]; // Now all non-zero elements // have been shifted to front // and 'count' is set as index // of first 0. Make all elements // 0 from count to end. while ($count < $n) $arr[$count++] = 0; } // Driver Code $arr = array(1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0, 9); $n = sizeof($arr); pushZerosToEnd($arr, $n); echo "Array after pushing all " . "zeros to end of array :\n"; for ($i = 0; $i < $n; $i++) echo $arr[$i] . " "; // This code is contributed // by ChitraNayal ?>
Javascript
<script> // A JavaScript program to move all zeroes at the end of array // Function which pushes all zeros to end of an array. function pushZerosToEnd(arr, n) { let count = 0; // Count of non-zero elements // Traverse the array. If element encountered is non- // zero, then replace the element at index 'count' // with this element for (let i = 0; i < n; i++) if (arr[i] != 0) arr[count++] = arr[i]; // here count is // incremented // Now all non-zero elements have been shifted to // front and 'count' is set as index of first 0. // Make all elements 0 from count to end. while (count < n) arr[count++] = 0; } // Driver code let arr = [1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0, 9]; let n = arr.length; pushZerosToEnd(arr, n); document.write("Array after pushing all zeros to end of array :<br>"); for (let i = 0; i < n; i++) document.write(arr[i] + " "); // This code is contributed by Surbhi Tyagi. </script>
C++
// C++ Program to move all zeros to the end #include <bits/stdc++.h> using namespace std; int main() { int A[] = { 5, 6, 0, 4, 6, 0, 9, 0, 8 }; int n = sizeof(A) / sizeof(A[0]); int j = 0; for (int i = 0; i < n; i++) { if (A[i] != 0) { swap(A[j], A[i]); // Partitioning the array j++; } } for (int i = 0; i < n; i++) { cout << A[i] << " "; // Print the array } return 0; }
C
// C Program to move all zeros to the end #include <stdio.h> int main() { int A[] = { 5, 6, 0, 4, 6, 0, 9, 0, 8 }; int n = sizeof(A) / sizeof(A[0]); int j = 0; for (int i = 0; i < n; i++) { if (A[i] != 0) { int temp = A[i]; // Partitioning the array A[i] = A[j]; A[j] = temp; j++; } } for (int i = 0; i < n; i++) { printf("%d ", A[i]); // Print the array } return 0; }
Java
// Java Program to move all zeros to the end import java.util.*; public class Main { public static void main(String[] args) { int[] A = { 5, 6, 0, 4, 6, 0, 9, 0, 8 }; int n = A.length; int j = 0; for (int i = 0; i < n; i++) { if (A[i] != 0) { // Swap - A[j] , A[i] swap(A, j, i); // Partitioning the array j++; } } for (int i = 0; i < n; i++) { System.out.print(A[i] + " "); // Print the array } } // Utility function to swap two elements of an array public static void swap(int[] A, int a, int b) { int temp = A[a]; A[a] = A[b]; A[b] = temp; } } // This code is contributed by tapeshdua420.
Python3
# Python Program to move all zeros to the end A = [5, 6, 0, 4, 6, 0, 9, 0, 8] n = len(A) j = 0 for i in range(n): if A[i] != 0: A[j], A[i] = A[i], A[j] # Partitioning the array j += 1 print(A) # Print the array # This code is contributed by Tapesh(tapeshdua420)
C#
using System; public static class GFG { // C# Program to move all zeros to the end public static void Main() { int[] A = { 5, 6, 0, 4, 6, 0, 9, 0, 8 }; int n = A.Length; int j = 0; for (int i = 0; i < n; i++) { if (A[i] != 0) { int temp = A[j]; A[j] = A[i]; A[i] = temp; j++; } } for (int i = 0; i < n; i++) { Console.Write(A[i]); Console.Write(" "); } } } // This code is contributed by Aarti_Rathi
Javascript
<script> // Javascript Program to move all zeros to the end let A = [5, 6, 0, 4, 6, 0, 9, 0, 8]; let n = A.length; let j = 0; for (let i = 0; i < n; i++) { if (A[i] != 0) { // Swap - A[j] , A[i] swap(A, j, i); // Partitioning the array j++; } } for (let i = 0; i < n; i++) { document.write(A[i] + " "); // Print the array } // Utility function to swap two elements of an array function swap(A, a, b) { let temp = A[a]; A[a] = A[b]; A[b] = temp; } // This code is contributed by Saurabh Jaiswal </script>
C++
// C++ program to shift all zeros // to right most side of array // whthout affecting order of non-zero // elements. #include <bits/stdc++.h> using namespace std; // function to shift zeros void move_zeros_to_right(vector<int>& m) { int count = 0; for (int i = 0; i < m.size(); i++) { if (m[i] == 0) { count++; // deleting the element from vector m.erase(m.begin() + i); i--; } } for (int i = 0; i < count; i++) { // inserting the zero into vector m.push_back(0); } cout << "array after shifting zeros to right side: " << endl; for (int i = 0; i < m.size(); i++) { // printing desired vector cout << m[i] << " "; } } // driver code int main() { vector<int> m{ 5, 6, 0, 4, 6, 0, 9, 0, 8 }; // function call move_zeros_to_right(m); return 0; } // This code is contributed by Machhaliya Muhammad
Java
// Java program to shift all zeros // to right most side of array // whthout affecting order of non-zero // elements. import java.util.*; class GFG { // function to shift zeros static void move_zeros_to_right(ArrayList<Integer> m) { int count = 0; for (int i = 0; i < m.size(); i++) { if (m.get(i) == 0) { count++; // deleting the element from vector m.remove(i); i--; } } for (int i = 0; i < count; i++) { // inserting the zero into arraylist m.add(0); } System.out.println("array after shifting zeros to right side: "); for (int i = 0; i < m.size(); i++) { // printing desired arraylist System.out.print(m.get(i) + " "); } } // Driver Code public static void main(String[] args) { ArrayList<Integer> m = new ArrayList<>(Arrays.asList(5, 6, 0, 4, 6, 0, 9, 0, 8)); // function call move_zeros_to_right(m); } } // This code is contributed by aditya942003patil
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA