Dado un gran número positivo como string, cuente todas las rotaciones del número dado que son divisibles por 4.
Ejemplos:
Input: 8 Output: 1 Input: 20 Output: 1 Rotation: 20 is divisible by 4 02 is not divisible by 4 Input : 13502 Output : 0 No rotation is divisible by 4 Input : 43292816 Output : 5 5 rotations are : 43292816, 16432928, 81643292 92816432, 32928164
Para números grandes, es difícil rotar y dividir cada número por 4. Por lo tanto, se usa la propiedad de ‘divisibilidad por 4’ que dice que un número es divisible por 4 si los últimos 2 dígitos del número son divisibles por 4 . Aquí en realidad no rotamos el número y verificamos la divisibilidad de los últimos 2 dígitos, sino que contamos pares consecutivos (en forma circular) que son divisibles por 4.
Ilustración:
Consider a number 928160 Its rotations are 928160, 092816, 609281, 160928, 816092, 281609. Now form pairs from the original number 928160 as mentioned in the approach. Pairs: (9,2), (2,8), (8,1), (1,6), (6,0), (0,9) We can observe that the 2-digit number formed by the these pairs, i.e., 92, 28, 81, 16, 60, 09, are present in the last 2 digits of some rotation. Thus, checking divisibility of these pairs gives the required number of rotations. Note: A single digit number can directly be checked for divisibility.
A continuación se muestra la implementación del enfoque.
C++
// C++ program to count all rotation divisible // by 4. #include <bits/stdc++.h> using namespace std; // Returns count of all rotations divisible // by 4 int countRotations(string n) { int len = n.length(); // For single digit number if (len == 1) { int oneDigit = n.at(0)-'0'; if (oneDigit%4 == 0) return 1; return 0; } // At-least 2 digit number (considering all // pairs) int twoDigit, count = 0; for (int i=0; i<(len-1); i++) { twoDigit = (n.at(i)-'0')*10 + (n.at(i+1)-'0'); if (twoDigit%4 == 0) count++; } // Considering the number formed by the pair of // last digit and 1st digit twoDigit = (n.at(len-1)-'0')*10 + (n.at(0)-'0'); if (twoDigit%4 == 0) count++; return count; } //Driver program int main() { string n = "4834"; cout << "Rotations: " << countRotations(n) << endl; return 0; }
Java
// Java program to count // all rotation divisible // by 4. import java.io.*; class GFG { // Returns count of all // rotations divisible // by 4 static int countRotations(String n) { int len = n.length(); // For single digit number if (len == 1) { int oneDigit = n.charAt(0)-'0'; if (oneDigit % 4 == 0) return 1; return 0; } // At-least 2 digit // number (considering all // pairs) int twoDigit, count = 0; for (int i = 0; i < (len-1); i++) { twoDigit = (n.charAt(i)-'0') * 10 + (n.charAt(i+1)-'0'); if (twoDigit%4 == 0) count++; } // Considering the number // formed by the pair of // last digit and 1st digit twoDigit = (n.charAt(len-1)-'0') * 10 + (n.charAt(0)-'0'); if (twoDigit%4 == 0) count++; return count; } //Driver program public static void main(String args[]) { String n = "4834"; System.out.println("Rotations: " + countRotations(n)); } } // This code is contributed by Nikita tiwari.
Python3
# Python3 program to count # all rotation divisible # by 4. # Returns count of all # rotations divisible # by 4 def countRotations(n) : l = len(n) # For single digit number if (l == 1) : oneDigit = (int)(n[0]) if (oneDigit % 4 == 0) : return 1 return 0 # At-least 2 digit number # (considering all pairs) count = 0 for i in range(0, l - 1) : twoDigit = (int)(n[i]) * 10 + (int)(n[i + 1]) if (twoDigit % 4 == 0) : count = count + 1 # Considering the number # formed by the pair of # last digit and 1st digit twoDigit = (int)(n[l - 1]) * 10 + (int)(n[0]) if (twoDigit % 4 == 0) : count = count + 1 return count # Driver program n = "4834" print("Rotations: " , countRotations(n)) # This code is contributed by Nikita tiwari.
C#
// C# program to count all rotation // divisible by 4. using System; class GFG { // Returns count of all // rotations divisible // by 4 static int countRotations(String n) { int len = n.Length; // For single digit number if (len == 1) { int oneDigit = n[0] - '0'; if (oneDigit % 4 == 0) return 1; return 0; } // At-least 2 digit // number (considering all // pairs) int twoDigit, count = 0; for (int i = 0; i < (len - 1); i++) { twoDigit = (n[i] - '0') * 10 + (n[i + 1] - '0'); if (twoDigit % 4 == 0) count++; } // Considering the number // formed by the pair of // last digit and 1st digit twoDigit = (n[len - 1] - '0') * 10 + (n[0] - '0'); if (twoDigit % 4 == 0) count++; return count; } //Driver program public static void Main() { String n = "4834"; Console.Write("Rotations: " + countRotations(n)); } } // This code is contributed by nitin mittal.
PHP
<?php // PHP program to count all // rotation divisible by 4. // Returns count of all // rotations divisible // by 4 function countRotations($n) { $len = strlen($n); // For single digit number if ($len == 1) { $oneDigit = $n[0] - '0'; if ($oneDigit % 4 == 0) return 1; return 0; } // At-least 2 digit // number (considering all // pairs) $twoDigit;$count = 0; for ($i = 0; $i < ($len - 1); $i++) { $twoDigit = ($n[$i] - '0') * 10 + ($n[$i + 1] - '0'); if ($twoDigit % 4 == 0) $count++; } // Considering the number // formed by the pair of // last digit and 1st digit $twoDigit = ($n[$len - 1] - '0') * 10 + ($n[0] - '0'); if ($twoDigit % 4 == 0) $count++; return $count; } // Driver Code $n = "4834"; echo "Rotations: " , countRotations($n); // This code is contributed by ajit ?>
Javascript
<script> // Javascript program to count all // rotation divisible by 4. // Returns count of all // rotations divisible // by 4 function countRotations(n) { let len = n.length; // For single digit number if (len == 1) { let oneDigit = n[0] - '0'; if (oneDigit % 4 == 0) return 1; return 0; } // At-least 2 digit // number (considering all // pairs) let twoDigit; let count = 0; for(let i = 0; i < (len - 1); i++) { twoDigit = (n[i] - '0') * 10 + (n[i + 1] - '0'); if (twoDigit % 4 == 0) count++; } // Considering the number // formed by the pair of // last digit and 1st digit twoDigit = (n[len - 1] - '0') * 10 + (n[0] - '0'); if (twoDigit % 4 == 0) count++; return count; } // Driver Code let n = "4834"; document.write("Rotations: " + countRotations(n)); // This code is contributed by _saurabh_jaiswal </script>
Rotations: 2
Complejidad de tiempo: O (n) donde n es el número de dígitos en el número de entrada.
Espacio Auxiliar: O(1)
Este artículo es una contribución de Ayush Jauhari . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA