Dada una array arr[] de N enteros, la tarea es intercambiar el primer y el último elemento, luego el tercero y el antepenúltimo elemento, luego el quinto y el quinto y así sucesivamente. Imprima la array final después de todas las operaciones válidas.
Entrada: arr[] = {1, 2, 3, 4, 5, 6}
Salida: 6 2 4 3 5 1
Operación 1: Intercambiar 1 y 6
Operación 2: Intercambiar 3 y 4
Entrada: arr[] = {5, 54, 12, 63, 45}
Salida: 45 54 12 63 5
Enfoque: inicialice el puntero i = 0 y j = N – 1, luego intercambie los elementos en estos punteros y actualice i = i + 2 y j = j – 2 . Repita estos pasos mientras i < j . Finalmente imprima la array actualizada.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Utility function to print // the contents of an array void printArr(int arr[], int n) { for (int i = 0; i < n; i++) cout << arr[i] << " "; } // Function to update the array void UpdateArr(int arr[], int n) { // Initialize the pointers int i = 0, j = n - 1; // While there are elements to swap while (i < j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; // Update the pointers i += 2; j -= 2; } // Print the updated array printArr(arr, n); } // Driver code int main() { int arr[] = { 1, 2, 3, 4, 5, 6 }; int n = sizeof(arr) / sizeof(arr[0]); UpdateArr(arr, n); return 0; }
Java
// Java implementation of the above approach class GFG { // Utility function to print // the contents of an array static void printArr(int arr[], int n) { for (int i = 0; i < n; i++) System.out.print(arr[i] + " "); } // Function to update the array static void UpdateArr(int arr[], int n) { // Initialize the pointers int i = 0, j = n - 1; // While there are elements to swap while (i < j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; // Update the pointers i += 2; j -= 2; } // Print the updated array printArr(arr, n); } // Driver code public static void main (String[] args) { int arr[] = { 1, 2, 3, 4, 5, 6 }; int n = arr.length; UpdateArr(arr, n); } } // This code is contributed by AnkitRai01
Python3
# Python3 implementation of the approach # Utility function to print # the contents of an array def printArr(arr, n): for i in range(n): print(arr[i], end = " "); # Function to update the array def UpdateArr(arr, n): # Initialize the pointers i = 0; j = n - 1; # While there are elements to swap while (i < j): temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; # Update the pointers i += 2; j -= 2; # Print the updated array printArr(arr, n); # Driver code if __name__ == '__main__': arr = [1, 2, 3, 4, 5, 6 ]; n = len(arr); UpdateArr(arr, n); # This code is contributed by PrinciRaj1992
C#
// C# implementation of the approach using System; class GFG { // Utility function to print // the contents of an array static void printArr(int []arr, int n) { for (int i = 0; i < n; i++) Console.Write(arr[i] + " "); } // Function to update the array static void UpdateArr(int []arr, int n) { // Initialize the pointers int i = 0, j = n - 1; // While there are elements to swap while (i < j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; // Update the pointers i += 2; j -= 2; } // Print the updated array printArr(arr, n); } // Driver code public static void Main (String[] args) { int []arr = { 1, 2, 3, 4, 5, 6 }; int n = arr.Length; UpdateArr(arr, n); } } // This code is contributed by 29AjayKumar
Javascript
<script> // JavaScript implementation of the approach // Utility function to print // the contents of an array function printArr(arr, n) { for (let i = 0; i < n; i++) document.write(arr[i] + " "); } // Function to update the array function UpdateArr(arr, n) { // Initialize the pointers let i = 0, j = n - 1; // While there are elements to swap while (i < j) { let temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; // Update the pointers i += 2; j -= 2; } // Print the updated array printArr(arr, n); } // Driver code let arr = [ 1, 2, 3, 4, 5, 6 ]; let n = arr.length;; UpdateArr(arr, n); // This code is contributed by Surbhi Tyagi </script>
6 2 4 3 5 1
Complejidad temporal: O(n)
Espacio auxiliar: O(1)