Dados dos números X e Y , la tarea es encontrar el número de enteros K que satisface la ecuación (X – 1) % K = (Y – 1) % K .
Ejemplos:
Entrada: X = 2, Y = 6
Salida: 3
Explicación:
K = {1, 2, 4} satisface la ecuación dada. Por lo tanto, la cuenta es 3.Entrada: X = 4, Y = 9
Salida: 2
Enfoque: siga los pasos a continuación para resolver el problema:
- La idea es encontrar la diferencia absoluta entre X e Y.
- Encuentre el conteo de divisores de la diferencia absoluta calculada e imprima el conteo como la respuesta requerida.
A continuación se muestra la implementación del enfoque anterior:
C++14
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to count integers K // satisfying given equation int condition(int a, int b) { // Calculate the absoluter // difference between a and b int d = abs(a - b), count = 0; // Iterate till sqrt of the difference for (int i = 1; i <= sqrt(d); i++) { if (d % i == 0) { if (d / i == i) count += 1; else count += 2; } } // Return the count return count; } // Driver Code int main() { int x = 2, y = 6; cout << condition(x, y) << endl; return 0; }
Java
// Java program for the above approach import java.io.*; class GFG{ // Function to count integers K // satisfying given equation static int condition(int a, int b) { // Calculate the absoluter // difference between a and b int d = Math.abs(a - b), count = 0; // Iterate till sqrt of the difference for(int i = 1; i <= Math.sqrt(d); i++) { if (d % i == 0) { if (d / i == i) count += 1; else count += 2; } } // Return the count return count; } // Driver Code public static void main (String[] args) { int x = 2, y = 6; System.out.println(condition(x, y)); } } // This code is contributed by sanjoy_62
Python3
# Python3 program for the above approach # Function to count integers K # satisfying given equation def condition(a, b): # Calculate the absoluter # difference between a and b d = abs(a - b) count = 0 # Iterate till sqrt of the difference for i in range(1, d + 1): if i * i > d: break if (d % i == 0): if (d // i == i): count += 1 else: count += 2 # Return the count return count # Driver Code if __name__ == '__main__': x = 2 y = 6 print(condition(x, y)) # This code is contributed by mohit kumar 29
C#
// C# program for the // above approach using System; class GFG{ // Function to count // integers K satisfying // given equation static int condition(int a, int b) { // Calculate the absoluter // difference between a and b int d = Math.Abs(a - b), count = 0; // Iterate till sqrt of // the difference for(int i = 1; i <= Math.Sqrt(d); i++) { if (d % i == 0) { if (d / i == i) count += 1; else count += 2; } } // Return the count return count; } // Driver Code public static void Main(String[] args) { int x = 2, y = 6; Console.WriteLine(condition(x, y)); } } // This code is contributed by shikhasingrajput
Javascript
<script> // Javascript program for the above approach // Function to count integers K // satisfying given equation function condition(a, b) { // Calculate the absoluter // difference between a and b let d = Math.abs(a - b), count = 0; // Iterate till sqrt of the difference for(let i = 1; i <= Math.sqrt(d); i++) { if (d % i == 0) { if (d / i == i) count += 1; else count += 2; } } // Return the count return count; } // Driver Code let x = 2, y = 6; document.write(condition(x, y)); </script>
Producción:
3
Complejidad de tiempo: O(√abs(X – Y))
Espacio auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por mohammadvaluda y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA