Dados tres números enteros N , X e Y , la tarea es encontrar el número entero positivo más grande K tal que K % X = Y donde 0 ≤ K ≤ N . Imprime -1 si no existe tal K.
Ejemplos:
Entrada: N = 15, X = 10, Y = 5
Salida: 15
Explicación:
Como 15 % 10 = 5Entrada: N = 187, X = 10, Y = 5
Salida: 185
Enfoque ingenuo: el enfoque más simple para resolver el problema es verificar para cada valor posible de K en el rango [0, N] , si la condición K % X = Y se cumple o no. Si no existe tal K , imprima -1 . De lo contrario, imprima el mayor valor posible de K del rango que satisfizo la condición.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find the largest // positive integer K such that K % x = y int findMaxSoln(int n, int x, int y) { // Stores the minimum solution int ans = INT_MIN; for (int k = 0; k <= n; k++) { if (k % x == y) { ans = max(ans, k); } } // Return the maximum possible value return ((ans >= 0 && ans <= n) ? ans : -1); } // Driver Code int main() { int n = 15, x = 10, y = 5; cout << findMaxSoln(n, x, y); return 0; }
Java
// Java program for the above approach import java.util.*; class GFG{ // Function to find the largest // positive integer K such that K % x = y static int findMaxSoln(int n, int x, int y) { // Stores the minimum solution int ans = Integer.MIN_VALUE; for(int k = 0; k <= n; k++) { if (k % x == y) { ans = Math.max(ans, k); } } // Return the maximum possible value return ((ans >= 0 && ans <= n) ? ans : -1); } // Driver Code public static void main(String[] args) { int n = 15, x = 10, y = 5; System.out.print(findMaxSoln(n, x, y)); } } // This code is contributed by Amit Katiyar
Python3
# Python3 program for the above approach import sys # Function to find the largest # positive integer K such that # K % x = y def findMaxSoln(n, x, y): # Stores the minimum solution ans = -sys.maxsize for k in range(n + 1): if (k % x == y): ans = max(ans, k) # Return the maximum possible value return (ans if (ans >= 0 and ans <= n) else -1) # Driver Code if __name__ == '__main__': n = 15 x = 10 y = 5 print(findMaxSoln(n, x, y)) # This code is contributed by Amit Katiyar
C#
// C# program for the above approach using System; class GFG{ // Function to find the largest // positive integer K such that // K % x = y static int findMaxSoln(int n, int x, int y) { // Stores the minimum solution int ans = int.MinValue; for(int k = 0; k <= n; k++) { if (k % x == y) { ans = Math.Max(ans, k); } } // Return the maximum possible value return ((ans >= 0 && ans <= n) ? ans : -1); } // Driver Code public static void Main(String[] args) { int n = 15, x = 10, y = 5; Console.Write(findMaxSoln(n, x, y)); } } // This code is contributed by shikhasingrajput
Javascript
<script> // Javascript program for the above approach // Function to find the largest // positive integer K such that K % x = y function findMaxSoln(n, x, y) { // Stores the minimum solution var ans = -1000000000; for (var k = 0; k <= n; k++) { if (k % x == y) { ans = Math.max(ans, k); } } // Return the maximum possible value return ((ans >= 0 && ans <= n) ? ans : -1); } // Driver Code var n = 15, x = 10, y = 5; document.write( findMaxSoln(n, x, y)); // This code is contributed by rrrtnx. </script>
15
Complejidad temporal: O(N)
Espacio auxiliar: O(1)
Enfoque eficiente: el enfoque anterior se puede optimizar en función de la observación de que K puede obtener uno de los siguientes valores para satisfacer la ecuación:
K = N – N % X + Y
o
K = N – N % X + Y – X
Siga los pasos a continuación para resolver el problema:
- Calcule K1 como K = N – N % X + Y y K2 como K = N – N%X + Y – X .
- Si K1 se encuentra en el rango [0, N] , imprima K1 .
- De lo contrario, si K2 se encuentra en el rango [0, N] , imprima K2 .
- De lo contrario, imprima -1 .
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 largest positive // integer K such that K % x = y int findMaxSoln(int n, int x, int y) { // Possible value of K as K1 if (n - n % x + y <= n) { return n - n % x + y; } // Possible value of K as K2 else { return n - n % x - (x - y); } } // Driver Code int main() { int n = 15, x = 10, y = 5; int ans = findMaxSoln(n, x, y); cout << ((ans >= 0 && ans <= n) ? ans : -1); }
Java
// Java program to implement // the above approach import java.util.*; class GFG{ // Function to find the largest positive // integer K such that K % x = y static int findMaxSoln(int n, int x, int y) { // Possible value of K as K1 if (n - n % x + y <= n) { return n - n % x + y; } // Possible value of K as K2 else { return n - n % x - (x - y); } } // Driver Code public static void main(String[] args) { int n = 15, x = 10, y = 5; int ans = findMaxSoln(n, x, y); System.out.print(((ans >= 0 && ans <= n) ? ans : -1)); } } // This code is contributed by Amit Katiyar
Python3
# Python3 program to implement # the above approach # Function to find the largest positive # integer K such that K % x = y def findMaxSoln(n, x, y): # Possible value of K as K1 if (n - n % x + y <= n): return n - n % x + y; # Possible value of K as K2 else: return n - n % x - (x - y); # Driver Code if __name__ == '__main__': n = 15; x = 10; y = 5; ans = findMaxSoln(n, x, y); print(( ans if (ans >= 0 and ans <= n) else -1)); # This code is contributed by 29AjayKumar
C#
// C# program to implement // the above approach using System; class GFG{ // Function to find the largest // positive integer K such that // K % x = y static int findMaxSoln(int n, int x, int y) { // Possible value of K as K1 if (n - n % x + y <= n) { return n - n % x + y; } // Possible value of K as K2 else { return n - n % x - (x - y); } } // Driver Code public static void Main(String[] args) { int n = 15, x = 10, y = 5; int ans = findMaxSoln(n, x, y); Console.Write(((ans >= 0 && ans <= n) ? ans : -1)); } } // This code is contributed by shikhasingrajput
Javascript
<script> // Javascript program to implement // the above approach // Function to find the largest positive // integer K such that K % x = y function findMaxSoln(n , x , y) { // Possible value of K as K1 if (n - n % x + y <= n) { return n - n % x + y; } // Possible value of K as K2 else { return n - n % x - (x - y); } } // Driver Code var n = 15, x = 10, y = 5; var ans = findMaxSoln(n, x, y); document.write( ((ans >= 0 && ans <= n) ? ans : -1) ); // This code contributed by umadevi9616 </script>
15
Tiempo Complejidad: O(1)
Espacio Auxiliar: O(1)