Dados dos números enteros N y K , la tarea es rotar los dígitos de N por K. Si K es un número entero positivo, rotar a la izquierda sus dígitos. De lo contrario, gire a la derecha sus dígitos.
Ejemplos:
Entrada: N = 12345, K = 2
Salida: 34512
Explicación
: Girar a la izquierda N(= 12345) por K(= 2) modifica N a 34512.
Por lo tanto, la salida requerida es 34512Entrada: N = 12345, K = -3
Salida: 34512
Explicación
: Girar a la derecha N(= 12345) por K( = -3) modifica N a 34512.
Por lo tanto, la salida requerida es 34512
Enfoque: siga los pasos a continuación para resolver el problema:
- Inicialice una variable, digamos X , para almacenar el recuento de dígitos en N.
- Actualice K = (K + X) % X para reducirlo a un caso de rotación a la izquierda.
- Elimine los primeros K dígitos de N y agregue todos los dígitos eliminados a la derecha de los dígitos de N .
- Finalmente, imprima el valor de N .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to find the count of // digits in N int numberOfDigit(int N) { // Stores count of // digits in N int digit = 0; // Calculate the count // of digits in N while (N > 0) { // Update digit digit++; // Update N N /= 10; } return digit; } // Function to rotate the digits of N by K void rotateNumberByK(int N, int K) { // Stores count of digits in N int X = numberOfDigit(N); // Update K so that only need to // handle left rotation K = ((K % X) + X) % X; // Stores first K digits of N int left_no = N / (int)(pow(10, X - K)); // Remove first K digits of N N = N % (int)(pow(10, X - K)); // Stores count of digits in left_no int left_digit = numberOfDigit(left_no); // Append left_no to the right of // digits of N N = (N * (int)(pow(10, left_digit))) + left_no; cout << N; } // Driver code int main() { int N = 12345, K = 7; // Function Call rotateNumberByK(N, K); return 0; } // The code is contributed by Dharanendra L V
Java
// Java program to implement // the above approach import java.io.*; class GFG { // Function to find the count of // digits in N static int numberOfDigit(int N) { // Stores count of // digits in N int digit = 0; // Calculate the count // of digits in N while (N > 0) { // Update digit digit++; // Update N N /= 10; } return digit; } // Function to rotate the digits of N by K static void rotateNumberByK(int N, int K) { // Stores count of digits in N int X = numberOfDigit(N); // Update K so that only need to // handle left rotation K = ((K % X) + X) % X; // Stores first K digits of N int left_no = N / (int)(Math.pow(10, X - K)); // Remove first K digits of N N = N % (int)(Math.pow(10, X - K)); // Stores count of digits in left_no int left_digit = numberOfDigit(left_no); // Append left_no to the right of // digits of N N = (N * (int)(Math.pow(10, left_digit))) + left_no; System.out.println(N); } // Driver Code public static void main(String args[]) { int N = 12345, K = 7; // Function Call rotateNumberByK(N, K); } }
Python3
# Python3 program to implement # the above approach # Function to find the count of # digits in N def numberOfDigit(N): # Stores count of # digits in N digit = 0 # Calculate the count # of digits in N while (N > 0): # Update digit digit += 1 # Update N N //= 10 return digit # Function to rotate the digits of N by K def rotateNumberByK(N, K): # Stores count of digits in N X = numberOfDigit(N) # Update K so that only need to # handle left rotation K = ((K % X) + X) % X # Stores first K digits of N left_no = N // pow(10, X - K) # Remove first K digits of N N = N % pow(10, X - K) # Stores count of digits in left_no left_digit = numberOfDigit(left_no) # Append left_no to the right of # digits of N N = N * pow(10, left_digit) + left_no print(N) # Driver Code if __name__ == '__main__': N, K = 12345, 7 # Function Call rotateNumberByK(N, K) # This code is contributed by mohit kumar 29
C#
// C# program to implement // the above approach using System; class GFG { // Function to find the count of // digits in N static int numberOfDigit(int N) { // Stores count of // digits in N int digit = 0; // Calculate the count // of digits in N while (N > 0) { // Update digit digit++; // Update N N /= 10; } return digit; } // Function to rotate the digits of N by K static void rotateNumberByK(int N, int K) { // Stores count of digits in N int X = numberOfDigit(N); // Update K so that only need to // handle left rotation K = ((K % X) + X) % X; // Stores first K digits of N int left_no = N / (int)(Math.Pow(10, X - K)); // Remove first K digits of N N = N % (int)(Math.Pow(10, X - K)); // Stores count of digits in left_no int left_digit = numberOfDigit(left_no); // Append left_no to the right of // digits of N N = (N * (int)(Math.Pow(10, left_digit))) + left_no; Console.WriteLine(N); } // Driver Code public static void Main(string []args) { int N = 12345, K = 7; // Function Call rotateNumberByK(N, K); } } // This code is contributed by AnkThon
Javascript
<script> // Javascript program to implement // the above approach // Function to find the count of // digits in N function numberOfDigit(N) { // Stores count of // digits in N let digit = 0; // Calculate the count // of digits in N while (N > 0) { // Update digit digit++; // Update N N = Math.floor( N / 10); } return digit; } // Function to rotate the digits of N by K function rotateNumberByK(N, K) { // Stores count of digits in N let X = numberOfDigit(N); // Update K so that only need to // handle left rotation K = ((K % X) + X) % X; // Stores first K digits of N let left_no = Math.floor (N / Math.floor(Math.pow(10, X - K))); // Remove first K digits of N N = N % Math.floor(Math.pow(10, X - K)); // Stores count of digits in left_no let left_digit = numberOfDigit(left_no); // Append left_no to the right of // digits of N N = (N * Math.floor(Math.pow(10, left_digit))) + left_no; document.write(N); } // Driver Code let N = 12345, K = 7; // Function Call rotateNumberByK(N, K); // This code is contributed by souravghosh0416. </script>
Producción:
34512
Complejidad de tiempo: O(log 10 N)
Espacio auxiliar: O(1)