Largest number less than or equal to Z that leaves a remainder X when divided by Y - GeeksforGeeks

01 Oct, 2021
Given three integers x, y, z, the task is to find the largest non-negative number less than or equal to z that leaves a remainder x when divided by y (Given x < y). If no such number exists then the output will be -1.
Examples: 
Input: x = 1, y = 5, z = 8
Output: 6
Explanation:
6 is the largest number less than 8 
which when divided by 5 
leaves a remainder 1.

Input: x = 4, y = 6, z = 3
Output: -1
Explanation:
Since no such number exists the output is -1
Approach: To solve the problem mentioned above the very first observation is if x > z then answer will not be possible, so output will be -1. Let the required number be p. Following are the two equations for solving the problem:  
p * y + x = 0
p * y <= (z – x)
In order to find the answer, we need to find the value of p. So, 
p = (z - x) / y
After calculating p we can simply find the answer which is  
p * y + x
Below is the implementation of the above approach: 
C++
Java
Python3
C#
Javascript
// C++ implementation to Find the// largest non-negative number that// is less than or equal to integer Z// and leaves a remainder X when divided by Y #include <bits/stdc++.h>using namespace std; // Function to get the numberint get(int x, int y, int z){     // remainder can't be larger    // than the largest number,    // if so then answer doesn't exist.    if (x > z)        return -1;     // reduce number by x    int val = z - x;     // finding the possible    // number that is divisible by y    int div = (z - x) / y;     // this number is always <= x    // as we calculated over z - x    int ans = div * y + x;     return ans;} // Driver Codeint main(){    // initialise the three integers    int x = 1, y = 5, z = 8;     cout << get(x, y, z) << "\n";     return 0;}
// Java implementation to Find the// largest non-negative number that// is less than or equal to integer Z// and leaves a remainder X when divided by Yclass GFG{  // Function to get the numberstatic int get(int x, int y, int z){      // remainder can't be larger    // than the largest number,    // if so then answer doesn't exist.    if (x > z)        return -1;      // reduce number by x    int val = z - x;      // finding the possible    // number that is divisible by y    int div = (z - x) / y;      // this number is always <= x    // as we calculated over z - x    int ans = div * y + x;      return ans;}  // Driver Codepublic static void main(String[] args){    // initialise the three integers    int x = 1, y = 5, z = 8;      System.out.print(get(x, y, z)+ "\n");}} // This code is contributed by sapnasingh4991
# Python implementation to Find the# largest non-negative number that# is less than or equal to integer Z# and leaves a remainder X when divided by Y # Function to get the numberdef get(x, y, z):         # remainder can't be larger    # than the largest number,    # if so then answer doesn't exist.    if (x > z):        return -1             # reduce number by x    val = z - x         # finding the possible    # number that is divisible by y    div = (z - x) // y              # this number is always <= x    # as we calculated over z - x    ans = div * y + x         return ans  # Driver Code# initialise the three integersx = 1y = 5z = 8 print(get(x, y, z)) # This code is contributed by shubhamsingh10
// C# implementation to Find the// largest non-negative number that// is less than or equal to integer Z// and leaves a remainder X when divided by Yusing System; class GFG{   // Function to get the numberstatic int get(int x, int y, int z){       // remainder can't be larger    // than the largest number,    // if so then answer doesn't exist.    if (x > z)        return -1;       // reduce number by x    int val = z - x;       // finding the possible    // number that is divisible by y    int div = (z - x) / y;       // this number is always <= x    // as we calculated over z - x    int ans = div * y + x;       return ans;}   // Driver Codepublic static void Main(String[] args){    // initialise the three integers    int x = 1, y = 5, z = 8;       Console.Write(get(x, y, z)+ "\n");}} // This code is contributed by sapnasingh4991
<script> // Javascript implementation to Find the// largest non-negative number that// is less than or equal to integer Z// and leaves a remainder X when divided by Y // Function to get the numberfunction get(x, y, z){       // remainder can't be larger    // than the largest number,    // if so then answer doesn't exist.    if (x > z)        return -1;       // reduce number by x    let val = z - x;       // finding the possible    // number that is divisible by y    let div = Math.floor((z - x) / y);       // this number is always <= x    // as we calculated over z - x    let ans = div * y + x;       return ans;} // Driver Code     // initialise the three integers    let x = 1, y = 5, z = 8;       document.write(get(x, y, z)+ "\n"); </script>
6
 
Time Complexity: O(1 )
Auxiliary Space: O(1)
sapnasingh4991
SHUBHAMSINGH10
ujjwalgoel1103
avijitmondal1998
arorakashish0911
Mathematical
School Programming
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Program to find GCD or HCF of two numbers
Sieve of Eratosthenes
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
C++ Classes and Objects