Find the largest possible value of K such that K modulo X is Y | C ++ program for the above approach ; Function to find the largest positive integer K such that K % x = y ; Stores the minimum solution ; Return the maximum possible value ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMaxSoln ( int n , int x , int y ) { int ans = INT_MIN ; for ( int k = 0 ; k <= n ; k ++ ) { if ( k % x == y ) { ans = max ( ans , k ) ; } } return ( ( ans >= 0 && ans <= n ) ? ans : -1 ) ; } int main ( ) { int n = 15 , x = 10 , y = 5 ; cout << findMaxSoln ( n , x , y ) ; return 0 ; }