Dada una array arr[] de tamaño N y un valor K, la tarea es imprimir la array rotada K veces a la derecha.
Ejemplos:
Entrada: arr = {1, 3, 5, 7, 9}, K = 2
Salida: 7 9 1 3 5Entrada: arr = {1, 2, 3, 4, 5}, K = 4
Salida: 2 3 4 5 1
Algoritmo: el problema dado se puede resolver invirtiendo los subarreglos . Se pueden seguir los siguientes pasos para resolver el problema:
- Invierta todos los elementos de la array de 1 a N -1
- Invierta los elementos de la array de 1 a K – 1
- Invierta los elementos de la array de K a N -1
C++
// C++ implementation for the above approach #include <bits/stdc++.h> using namespace std; // Function to rotate the array // to the right, K times void RightRotate(int Array[], int N, int K) { // Reverse all the array elements reverse(Array, Array + N); // Reverse the first k elements reverse(Array, Array + K); // Reverse the elements from K // till the end of the array reverse(Array + K, Array + N); // Print the array after rotation for (int i = 0; i < N; i++) { cout << Array[i] << " "; } cout << endl; } // Driver code int main() { // Initialize the array int Array[] = { 1, 2, 3, 4, 5 }; // Find the size of the array int N = sizeof(Array) / sizeof(Array[0]); // Initialize K int K = 4; // Call the function and // print the answer RightRotate(Array, N, K); return 0; }
Java
/*package whatever //do not write package name here */ import java.io.*; class GFG { // Function to rotate the array // to the right, K times static void RightRotate(int[] Array, int N, int K) { // Reverse all the array elements for (int i = 0; i < N / 2; i++) { int temp = Array[i]; Array[i] = Array[N - i - 1]; Array[N - i - 1] = temp; } // Reverse the first k elements for (int i = 0; i < K / 2; i++) { int temp = Array[i]; Array[i] = Array[K - i - 1]; Array[K - i - 1] = temp; } // Reverse the elements from K // till the end of the array for (int i = 0; i < (K + N) / 2; i++) { int temp = Array[(i + K) % N]; Array[(i + K) % N] = Array[(N - i + K - 1) % N]; Array[(N - i + K - 1) % N] = temp; } // Print the array after rotation for (int i = 0; i < N; i++) { System.out.print(Array[i] + " "); } System.out.println(); } // Driver code public static void main(String[] args) { // Initialize the array int Array[] = { 1, 2, 3, 4, 5 }; // Find the size of the array int N = Array.length; // Initialize K int K = 4; // Call the function and // print the answer RightRotate(Array, N, K); } } // This code is contributed by maddler.
Python3
# Python program for the above approach import math # Function to rotate the array # to the right, K times def RightRotate(Array, N, K): # Reverse all the array elements for i in range(math.ceil(N / 2)): temp = Array[i] Array[i] = Array[N - i - 1] Array[N - i - 1] = temp # Reverse the first k elements for i in range(math.ceil(K / 2)): temp = Array[i] Array[i] = Array[K - i - 1] Array[K - i - 1] = temp # Reverse the elements from K # till the end of the array for i in range(math.ceil((K + N) / 2)): temp = Array[(i + K) % N] Array[(i + K) % N] = Array[(N - i + K - 1) % N] Array[(N - i + K - 1) % N] = temp # Print the array after rotation for i in range(N): print(Array[i], end=" ") # Driver Code arr = [1, 2, 3, 4, 5] N = len(arr) K = 4 # Call the function and # print the answer RightRotate(arr, N, K) # This code is contributed by Saurabh Jaiswal
C#
// C# program for the above approach using System; class GFG { // Function to rotate the array // to the right, K times static void RightRotate(int []Array, int N, int K) { // Reverse all the array elements for (int i = 0; i < N / 2; i++) { int temp = Array[i]; Array[i] = Array[N - i - 1]; Array[N - i - 1] = temp; } // Reverse the first k elements for (int i = 0; i < K / 2; i++) { int temp = Array[i]; Array[i] = Array[K - i - 1]; Array[K - i - 1] = temp; } // Reverse the elements from K // till the end of the array for (int i = 0; i < (K + N) / 2; i++) { int temp = Array[(i + K) % N]; Array[(i + K) % N] = Array[(N - i + K - 1) % N]; Array[(N - i + K - 1) % N] = temp; } // Print the array after rotation for (int i = 0; i < N; i++) { Console.Write(Array[i] + " "); } } // Driver code public static void Main() { // Initialize the array int []Array = { 1, 2, 3, 4, 5 }; // Find the size of the array int N = Array.Length; // Initialize K int K = 4; // Call the function and // print the answer RightRotate(Array, N, K); } } // This code is contributed by Samim Hossain Mondal.
Javascript
<script> // Javascript program for the above approach // Function to rotate the array // to the right, K times function RightRotate(Array, N, K) { // Reverse all the array elements for (let i = 0; i < N / 2; i++) { let temp = Array[i]; Array[i] = Array[N - i - 1]; Array[N - i - 1] = temp; } // Reverse the first k elements for (let i = 0; i < K / 2; i++) { let temp = Array[i]; Array[i] = Array[K - i - 1]; Array[K - i - 1] = temp; } // Reverse the elements from K // till the end of the array for (let i = 0; i < (K + N) / 2; i++) { let temp = Array[(i + K) % N]; Array[(i + K) % N] = Array[(N - i + K - 1) % N]; Array[(N - i + K - 1) % N] = temp; } // Print the array after rotation for (let i = 0; i < N; i++) { document.write(Array[i] + " "); } } // Driver Code let arr = [ 1, 2, 3, 4, 5 ]; let N = arr.length; let K =4; // Call the function and // print the answer RightRotate(arr, N, K); // This code is contributed by Samim Hossain Mondal. </script>
Producción
2 3 4 5 1
Complejidad de Tiempo : O(N)
Espacio Auxiliar : O(1)