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

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 = 8Output:6Explanation:6 is the largest number less than 8 which when divided by 5 leaves a remainder 1.Input:x = 4, y = 6, z = 3Output:-1Explanation: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++

`// 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 number ` `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 Code ` `int` `main() ` `{ ` ` ` `// initialise the three integers ` ` ` `int` `x = 1, y = 5, z = 8; ` ` ` ` ` `cout << get(x, y, z) << ` `"\n"` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// 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 Y ` `class` `GFG{ ` ` ` `// Function to get the number ` `static` `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 Code ` `public` `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 ` |

*chevron_right*

*filter_none*

## Python3

`# 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 number ` `def` `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 integers ` `x ` `=` `1` `y ` `=` `5` `z ` `=` `8` ` ` `print` `(get(x, y, z)) ` ` ` `# This code is contributed by shubhamsingh10 ` |

*chevron_right*

*filter_none*

## C#

`// 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 ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to get the number ` `static` `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 Code ` `public` `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 ` |

*chevron_right*

*filter_none*

**Output:**

6

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Largest number by which given 3 numbers should be divided such that they leaves same remainder
- Program to find remainder when large number is divided by 11
- Program to find remainder when large number is divided by r
- Minimum number of operations on a binary string such that it gives 10^A as remainder when divided by 10^B
- Find the remainder when First digit of a number is divided by its Last digit
- Minimum integer such that it leaves a remainder 1 on dividing with any element from the range [2, N]
- Check if the remainder of N-1 factorial when divided by N is N-1 or not
- Find the remainder when N is divided by 4 using Bitwise AND operator
- Count of integers obtained by replacing ? in the given string that give remainder 5 when divided by 13
- Largest number less than or equal to N/2 which is coprime to N
- Largest number smaller than or equal to N divisible by K
- Sum of largest prime factor of each number less than equal to n
- Find Largest Special Prime which is less than or equal to a given number
- Largest number smaller than or equal to n and digits in non-decreasing order
- Largest proper fraction with sum of numerator and denominator equal to a given number
- Check if an array of 1s and 2s can be divided into 2 parts with equal sum
- Find the sums for which an array can be divided into sub-arrays of equal sum
- Find minimum number to be divided to make a number a perfect square
- Count of Array elements to be divided by 2 to make at least K elements equal
- Program for quotient and remainder of big number

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.