Dada una array arr[] de tamaño N y un número entero K, la tarea es girar a la derecha la array K veces.
Ejemplos:
Entrada: arr[] = {1, 3, 5, 7, 9}, K = 2
Salida: 7 9 1 3 5
Explicación: Después de la primera rotación: {9, 1, 3, 5, 7}
Después de la segunda rotación: { 7, 9, 1, 3, 5}Entrada: {1, 2, 3, 4, 5, 6}, K = 2
Salida: 5 6 1 2 3 4
Enfoque: El enfoque ingenuo y el enfoque basado en la inversión de partes de la array se analizan aquí .
Enfoque basado en punteros: la base de este concepto es el algoritmo de inversión para la rotación de arrays. La array se divide en dos partes donde la primera parte es de tamaño (NK) y la parte final es de tamaño K. Estas dos partes se invierten individualmente. Luego se invierte todo el arreglo.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ code to implement above approach #include <iostream> using namespace std; // Function to print the array void print(int arr[], int N) { for (int i = 0; i < N; i++) cout << *(arr + i) << " "; } // Function to reverse the array // from start to end index void reverse(int arr[], int start, int end) { int temp; int size = end - start; // Reversal based on pointer approach for (int i = 0; i < (size / 2); i++) { temp = *(arr + i + start); *(arr + i + start) = *(arr + start + size - i - 1); *(arr + start + size - i - 1) = temp; } } // Function to right rotate the array K times void right(int arr[], int K, int N) { reverse(arr, 0, N - K); reverse(arr, N - K, N); reverse(arr, 0, N); print(arr, N); } // Driver code int main() { int arr[] = { 1, 2, 3, 4, 5, 6 }; int N = sizeof(arr) / sizeof(arr[0]); int K = 2; right(arr, K, N); return 0; }
Java
// Java code to implement above approach import java.util.*; class GFG{ // Function to print the array static void print(int arr[], int N) { for (int i = 0; i < N; i++) System.out.print(arr[i]+ " "); } // Function to reverse the array // from start to end index static int[] reverse(int arr[], int start, int end) { int temp; int size = end - start; // Reversal based on pointer approach for (int i = 0; i < (size / 2); i++) { temp = arr[ i + start]; arr[i + start] = arr[start + size - i - 1]; arr[start + size - i - 1] = temp; } return arr; } // Function to right rotate the array K times static void right(int arr[], int K, int N) { arr = reverse(arr, 0, N - K); arr = reverse(arr, N - K, N); arr = reverse(arr, 0, N); print(arr, N); } // Driver code public static void main(String[] args) { int arr[] = { 1, 2, 3, 4, 5, 6 }; int N = arr.length; int K = 2; right(arr, K, N); } } // This code is contributed by 29AjayKumar
Python3
# Python code to implement above approach # Function to print array def print1(arr, N): for i in range(N): print(arr[i], end = " "); # Function to reverse the array # from start to end index def reverse(arr, start, end): temp = 0; size = end - start; # Reversal based on pointer approach for i in range(size//2): temp = arr[i + start]; arr[i + start] = arr[start + size - i - 1]; arr[start + size - i - 1] = temp; return arr; # Function to right rotate the array K times def right(arr, K, N): arr = reverse(arr, 0, N - K); arr = reverse(arr, N - K, N); arr = reverse(arr, 0, N); print1(arr, N); # Driver code if __name__ == '__main__': arr = [1, 2, 3, 4, 5, 6]; N = len(arr); K = 2; right(arr, K, N); # This code is contributed by 29AjayKumar
C#
// C# code to implement above approach using System; class GFG { // Function to print the array static void print(int[] arr, int N) { for (int i = 0; i < N; i++) Console.Write(arr[i] + " "); } // Function to reverse the array // from start to end index static void reverse(int[] arr, int start, int end) { int temp; int size = end - start; // Reversal based on pointer approach for (int i = 0; i < (size / 2); i++) { temp = arr[i + start]; arr[i + start] = arr[start + size - i - 1]; arr[start + size - i - 1] = temp; } } // Function to right rotate the array K times static void right(int[] arr, int K, int N) { reverse(arr, 0, N - K); reverse(arr, N - K, N); reverse(arr, 0, N); print(arr, N); } // Driver code public static void Main() { int[] arr = { 1, 2, 3, 4, 5, 6 }; int N = arr.Length; int K = 2; right(arr, K, N); } } // This code is contributed by ukasp.
Javascript
<script> // JavaScript code to implement above approach // Function to print the array const print = (arr, N) => { for (let i = 0; i < N; i++) document.write(`${arr[i]} `); } // Function to reverse the array // from start to end index const reverse = (arr, start, end) => { let temp; let size = end - start; // Reversal based on pointer approach for (let i = 0; i < parseInt(size / 2); i++) { temp = arr[i + start]; arr[i + start] = arr[start + size - i - 1]; arr[start + size - i - 1] = temp; } } // Function to right rotate the array K times const right = (arr, K, N) => { reverse(arr, 0, N - K); reverse(arr, N - K, N); reverse(arr, 0, N); print(arr, N); } // Driver code let arr = [1, 2, 3, 4, 5, 6]; let N = arr.length; let K = 2; right(arr, K, N); // This code is contributed by rakeshsahni </script>
5 6 1 2 3 4
Complejidad temporal: O(N)
Espacio auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por meenakshi mehta y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA