Dado un entero n , la tarea es encontrar dos enteros a y b que satisfagan las siguientes condiciones:
- un % b = 0
- a * b > n
- un / segundo < norte
Si ningún par satisface las condiciones anteriores, imprima -1 .
Nota: Puede haber varios pares (a, b) que satisfagan las condiciones anteriores para n .
Ejemplos:
Input: n = 10 Output: a = 90, b = 10 90 % 10 = 0 90 * 10 = 900 > 10 90 / 10 = 9 < 10 All three conditions are satisfied. Input: n = 1 Output: -1
Enfoque: supongamos que b = n , al tomar esta suposición , se puede encontrar a en función de las condiciones dadas:
- (a % b = 0) => a debe ser múltiplo de b .
- (a / b < n) => a / b = n – 1 que es < n .
- (un * segundo > norte) => un = norte .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the above approach #include <bits/stdc++.h> using namespace std; // Function to print the required numbers void find(int n) { // Suppose b = n and we want a % b = 0 and also // (a / b) < n so a = b * (n - 1) int b = n; int a = b * (n - 1); // Special case if n = 1 // we get a = 0 so (a * b) < n if (a * b > n && a / b < n) { cout << "a = " << a << ", b = " << b; } // If no pair satisfies the conditions else cout << -1 << endl; } // Driver code int main() { int n = 10; find(n); return 0; }
Java
// Java implementation of the above approach public class GFG{ // Function to print the required numbers static void find(int n) { // Suppose b = n and we want a % b = 0 and also // (a / b) < n so a = b * (n - 1) int b = n; int a = b * (n - 1); // Special case if n = 1 // we get a = 0 so (a * b) < n if (a * b > n && a / b < n) { System.out.print("a = " + a + ", b = " + b); } // If no pair satisfies the conditions else System.out.println(-1); } // Driver code public static void main(String []args) { int n = 10; find(n); } // This code is contributed by Ryuga }
Python3
# Python3 implementation of the above approach # Function to print the required numbers def find(n): # Suppose b = n and we want a % b = 0 # and also (a / b) < n so a = b * (n - 1) b = n a = b * (n - 1) # Special case if n = 1 # we get a = 0 so (a * b) < n if a * b > n and a // b < n: print("a = {}, b = {}" . format(a, b)) # If no pair satisfies the conditions else: print(-1) # Driver Code if __name__ == "__main__": n = 10 find(n) # This code is contributed by Rituraj Jain
C#
// C# implementation of the above approach using System; class GFG { // Function to print the required numbers static void find(int n) { // Suppose b = n and we want a % b = 0 // and also (a / b) < n so a = b * (n - 1) int b = n; int a = b * (n - 1); // Special case if n = 1 // we get a = 0 so (a * b) < n if (a * b > n && a / b < n) { Console.Write("a = " + a + ", b = " + b); } // If no pair satisfies the conditions else Console.WriteLine(-1); } // Driver code public static void Main() { int n = 10; find(n); } } // This code is contributed // by Akanksha Rai
PHP
<?php // PHP implementation of the above approach // Function to print the required numbers function find($n) { // Suppose b = n and we want a % b = 0 and also // (a / b) < n so a = b * (n - 1) $b = $n; $a = $b * ($n - 1); // Special case if n = 1 // we get a = 0 so (a * b) < n if ($a * $b > $n && $a / $b <$n) { echo "a = " , $a , ", b = " , $b; } // If no pair satisfies the conditions else echo -1 ; } // Driver code $n = 10; find($n); // This code is contributed // by inder_verma.. ?>
Javascript
<script> // Javascript implementation of the above approach // Function to print the required numbers function find(n) { // Suppose b = n and we want a % b = 0 // and also (a / b) < n so a = b * (n - 1) let b = n; let a = b * (n - 1); // Special case if n = 1 // we get a = 0 so (a * b) < n if (a * b > n && a / b < n) { document.write("a = " + a + ", b = " + b); } // If no pair satisfies the conditions else document.write(-1); } let n = 10; find(n); // This code is contributed by surehs07. </script>
Producción:
a = 90, b = 10
Complejidad Temporal: O(1), ya que no hay bucle ni recursividad.
Espacio Auxiliar: O(1), ya que no se ha ocupado ningún espacio extra.
Publicación traducida automáticamente
Artículo escrito por sahilshelangia y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA