Find the largest possible value of K such that K modulo X is Y | Java 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
import java . util . * ; class GFG { static int findMaxSoln ( int n , int x , int y ) { int ans = Integer . MIN_VALUE ; for ( int k = 0 ; k <= n ; k ++ ) { if ( k % x == y ) { ans = Math . max ( ans , k ) ; } } return ( ( ans >= 0 && ans <= n ) ? ans : - 1 ) ; } public static void main ( String [ ] args ) { int n = 15 , x = 10 , y = 5 ; System . out . print ( findMaxSoln ( n , x , y ) ) ; } }