Dada una array de enteros, separe los números pares e impares en la array. Todos los números pares deben estar presentes primero y luego los números impares.
Ejemplos:
Input : 1 9 5 3 2 6 7 11 Output : 6 2 3 5 7 9 11 1 Input : 1 3 2 4 7 6 9 10 Output : 10 2 6 4 7 9 3 1
Hemos discutido un enfoque en Segregar números pares e impares . En esta publicación, se analiza un enfoque diferente más simple que utiliza una array adicional.
Enfoque:
1. Inicie dos punteros desde la izquierda y la derecha de la array.
2. Cree una nueva array del mismo tamaño que se indica.
3. Si el elemento a la izquierda o a la derecha es par, colóquelo al frente de la array al final.
C++
// CPP code to segregate even odd // numbers in an array #include <bits/stdc++.h> using namespace std; // Function to segregate even odd numbers void arrayEvenAndOdd(int arr[], int n) { int b[n]; // To store result int k = 0, l = n - 1, i, j; for (i = 0, j = n - 1; i < j; i++, j--) { if (arr[i] % 2 == 0) { b[k] = arr[i]; k++; } else { b[l] = arr[i]; l--; } if (arr[j] % 2 == 0) { b[k] = arr[j]; k++; } else { b[l] = arr[j]; l--; } } // for i == j in case of odd length b[i] = arr[i]; // Printing segregated array for (int i = 0; i < n; i++) cout << b[i] << " "; } // Driver code int main() { int arr[] = {1, 3, 2, 4, 7, 6, 9, 10}; int n = sizeof(arr) / sizeof(int); arrayEvenAndOdd(arr, n); return 0; }
Java
// Java code to segregate even odd // numbers in an array import java.util.Arrays; class arr { // Function to segregate even odd numbers static void arrayEvenAndOdd(int arr[], int n) { // To store result int b[] = new int[n]; int k = 0, l = n - 1, i, j; for (i = 0, j = n - 1; i < j; i++, j--) { if (arr[i] % 2 == 0) { b[k] = arr[i]; k++; } else { b[l] = arr[i]; l--; } if (arr[j] % 2 == 0) { b[k] = arr[j]; k++; } else { b[l] = arr[j]; l--; } } // for i == j in case of odd length b[i] = arr[i]; // Printing segregated array for (i = 0; i < n; i++) { System.out.print(b[i] + " "); } } // Driver code public static void main(String[] args) { int arr[] = {1, 3, 2, 4, 7, 6, 9, 10}; int n = arr.length; arrayEvenAndOdd(arr, n); } } // This code is contributed // by Smitha Dinesh Semwal
Python3
# Python3 code to segregate even odd # numbers in an array # Function to segregate even odd numbers def arrayEvenAndOdd(arr,n): b = [0] * n # To store result k = 0 l = n - 1 i = 0 j = n-1 while(i < j): if (arr[i] % 2 == 0): b[k] = arr[i] k+=1 else: b[l] = arr[i] l-=1 if (arr[j] % 2 == 0): b[k] = arr[j] k+=1 else: b[l] = arr[j] l-=1 i+=1 j-=1 # for i == j in case of odd length b[i] = arr[i] # Printing segregated array for i in range(0, n): print(b[i],end=" ") # Driver code arr = [1, 3, 2, 4, 7, 6, 9, 10] n = len(arr) arrayEvenAndOdd(arr, n) # This code is contributed by # Smitha Dinesh Semwal
C#
// C# code to segregate even odd // numbers in an array using System; class GFG { // Function to segregate even odd numbers static void arrayEvenAndOdd(int []arr, int n) { // To store result int []b = new int[n]; int k = 0, l = n - 1, i, j; for (i = 0, j = n - 1; i < j; i++, j--) { if (arr[i] % 2 == 0) { b[k] = arr[i]; k++; } else { b[l] = arr[i]; l--; } if (arr[j] % 2 == 0) { b[k] = arr[j]; k++; } else { b[l] = arr[j]; l--; } } // for i == j in case of odd length b[i] = arr[i]; // Printing segregated array for (i = 0; i < n; i++) { Console.Write(b[i] + " "); } } // Driver code public static void Main() { int []arr = {1, 3, 2, 4, 7, 6, 9, 10}; int n = arr.Length; arrayEvenAndOdd(arr, n); } } // This code is contributed by vt_m.
Javascript
<script> // Java scriptcode to segregate even odd // numbers in an array // Function to segregate even odd numbers function arrayEvenAndOdd(arr,n) { // To store result let b=[n]; let k = 0, l = n - 1, i, j; for (i = 0, j = n - 1; i < j; i++, j--) { if (arr[i] % 2 == 0) { b[k] = arr[i]; k++; } else { b[l] = arr[i]; l--; } if (arr[j] % 2 == 0) { b[k] = arr[j]; k++; } else { b[l] = arr[j]; l--; } } // for i == j in case of odd length b[i] = arr[i]; // Printing segregated array for (i = 0; i < n; i++) { document.write(b[i] + " "); } } // Driver code let arr = [1, 3, 2, 4, 7, 6, 9, 10]; let n = arr.length; arrayEvenAndOdd(arr, n); // This code is contributed by sravan kumar Gottumukkala </script>
10 2 6 4 7 9 3 1
Complejidad de Tiempo : O(n/2)
Espacio Auxiliar : O(n)
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