Dada una array arr[] de tamaño N , la tarea es ordenar todos los números pares de la array, sin cambiar el orden de los elementos impares
Ejemplos :
Entrada : arr[] = {4, 7, 2, 11, 15}
Salida : {2, 7, 4, 11, 15}
Explicación : los números pares se ordenan en sus lugares correspondientes, sin cambiar el orden de los elementos impares
Entrada : arr[] = {12, 6}
Salida : {6, 12}
Enfoque : la tarea se puede resolver segregando los elementos pares en otro contenedor, digamos ‘ evens ‘, clasifique este contenedor y, mientras itera la array, reemplace los elementos pares con los elementos pares ordenados almacenados en pares.
A continuación se muestra la implementación del enfoque anterior:
C++
#include <bits/stdc++.h> using namespace std; // Function to get the required array void solve(int arr[], int n) { // Store even elements vector<int> evens; for (int i = 0; i < n; i++) { if (arr[i] % 2 == 0) evens.push_back(arr[i]); } // Sort all even elements sort(evens.begin(), evens.end()); int l = 0; // Placing even elements in sorted order for (int i = 0; i < n; i++) { if (arr[i] % 2 == 0) arr[i] = evens[l++]; } // Updated array for (int i = 0; i < n; i++) cout << arr[i] << " "; } // Driver Code int main() { int N = 5; int arr[N] = { 4, 7, 2, 11, 15 }; solve(arr, N); return 0; }
Java
// Java program for the above approach import java.util.*; public class GFG { // Function to get the required array static void solve(int []arr, int n) { // Store even elements ArrayList<Integer> evens = new ArrayList<Integer>(); for (int i = 0; i < n; i++) { if (arr[i] % 2 == 0) evens.add(arr[i]); } // Sort all even elements Collections.sort(evens); int l = 0; // Placing even elements in sorted order for (int i = 0; i < n; i++) { if (arr[i] % 2 == 0) arr[i] = evens.get(l++); } // Updated array for (int i = 0; i < n; i++) System.out.print(arr[i] + " "); } // Driver function public static void main(String []args) { int N = 5; int []arr = { 4, 7, 2, 11, 15 }; solve(arr, N); } } // This code is contributed by AnkThon
Python3
# python program of the above approach # Function to get the required array def solve(arr, n): # Store even elements evens = [] for i in range(0, n): if (arr[i] % 2 == 0): evens.append(arr[i]) # Sort all even elements evens.sort() l = 0 # Placing even elements in sorted order for i in range(0, n): if (arr[i] % 2 == 0): arr[i] = evens[l] l += 1 # Updated array for i in range(0, n): print(arr[i], end=" ") # Driver Code if __name__ == "__main__": N = 5 arr = [4, 7, 2, 11, 15] solve(arr, N) # This code is contributed by rakeshsahni
C#
// C# program for the above approach using System; using System.Collections.Generic; class GFG { // Function to get the required array static void solve(int []arr, int n) { // Store even elements List<int> evens = new List<int>(); for (int i = 0; i < n; i++) { if (arr[i] % 2 == 0) evens.Add(arr[i]); } // Sort all even elements evens.Sort(); int l = 0; // Placing even elements in sorted order for (int i = 0; i < n; i++) { if (arr[i] % 2 == 0) arr[i] = evens[l++]; } // Updated array for (int i = 0; i < n; i++) Console.Write(arr[i] + " "); } // Driver function public static void Main() { int N = 5; int []arr = new int[] { 4, 7, 2, 11, 15 }; solve(arr, N); } } // This code is contributed by Samim Hossain Mondal
Javascript
<script> // Javascript program for the above approach // Function to get the required array function solve(arr, n) { // Store even elements let evens = []; for (let i = 0; i < n; i++) { if (arr[i] % 2 == 0) evens.push(arr[i]); } // Sort all even elements evens.sort(); let l = 0; // Placing even elements in sorted order for (let i = 0; i < n; i++) { if (arr[i] % 2 == 0) arr[i] = evens[l++]; } // Updated array for (let i = 0; i < n; i++) document.write(arr[i] + " "); } // Driver Code let N = 5; let arr = [ 4, 7, 2, 11, 15 ]; solve(arr, N); // This code is contributed by Samim Hossain Mondal. </script>
2 7 4 11 15
Complejidad de tiempo : O(NlogN)
Espacio auxiliar : O(N)
Publicación traducida automáticamente
Artículo escrito por himanshuverma11 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA