Open In App

Largest number up to N whose modulus with X is equal to Y modulo X

Given three positive integers X, Y, and N, such that Y < X, the task is to find the largest number from the range [0, N] whose modulus with X is equal to Y modulo X.

Examples:



Input: X = 10, Y = 5, N = 15
Output: 15
Explanation:
The value of 15 % 10 (= 5) and 5 % 10 (= 5) are equal.
Therefore, the required output is 15.

Input: X = 5, Y = 0, N = 4
Output: 0



Approach: The given problem can be solved based on the following observations:

Follow the steps below to solve the problem:

Below is the implementation of the above approach:




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to print the largest
// number upto N whose modulus
// with X is same as Y % X
long long maximumNum(long long X,
                     long long Y,
                     long long N)
{
    // Stores the required number
    long long num = 0;
 
    // Update num as the result
    if (N - N % X + Y <= N) {
 
        num = N - N % X + Y;
    }
    else {
        num = N - N % X - (X - Y);
    }
 
    // Return the resultant number
    return num;
}
 
// Driver Code
int main()
{
    long long X = 10;
    long long Y = 5;
    long long N = 15;
 
    cout << maximumNum(X, Y, N);
 
    return 0;
}




// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG
{
 
  // Function to print the largest
  // number upto N whose modulus
  // with X is same as Y % X
  static long maximumNum(long X, long Y, long N)
  {
     
    // Stores the required number
    long num = 0;
 
    // Update num as the result
    if (N - N % X + Y <= N)
    {
      num = N - N % X + Y;
    }
    else
    {
      num = N - N % X - (X - Y);
    }
 
    // Return the resultant number
    return num;
  }
 
  // Driver Code
  public static void main(String[] args)
  {
 
    long X = 10;
    long Y = 5;
    long N = 15;
 
    System.out.println(maximumNum(X, Y, N));
  }
}
 
// This code is contributed by Kingash.




# Python3 program for the above approach
 
# Function to print the largest
# number upto N whose modulus
# with X is same as Y % X
def maximumNum(X, Y, N):
   
    # Stores the required number
    num = 0
 
    # Update num as the result
    if (N - N % X + Y <= N):
        num = N - N % X + Y
    else:
        num = N - N % X - (X - Y)
 
    # Return the resultant number
    return num
 
# Driver Code
if __name__ == '__main__':
    X = 10
    Y = 5
    N = 15
 
    print (maximumNum(X, Y, N))
 
# This code is contributed by mohit kumar 29.




// C# program for the above approach
using System;
class GFG {
 
  // Function to print the largest
  // number upto N whose modulus
  // with X is same as Y % X
  static long maximumNum(long X, long Y, long N)
  {
 
    // Stores the required number
    long num = 0;
 
    // Update num as the result
    if (N - N % X + Y <= N) {
      num = N - N % X + Y;
    }
    else {
      num = N - N % X - (X - Y);
    }
 
    // Return the resultant number
    return num;
  }
 
  // Driver Code
  public static void Main(string[] args)
  {
 
    long X = 10;
    long Y = 5;
    long N = 15;
 
    Console.WriteLine(maximumNum(X, Y, N));
  }
}
 
// This code is contributed by ukasp.




<script>
 
// Javascript program for the above approach
 
// Function to print the largest
// number upto N whose modulus
// with X is same as Y % X
function maximumNum(X, Y, N)
{
 
    // Stores the required number
    let num = 0;
     
    // Update num as the result
    if (N - N % X + Y <= N)
    {
        num = N - N % X + Y;
    }
    else
    {
        num = N - N % X - (X - Y);
    }
     
    // Return the resultant number
    return num;
}
 
// Driver code
let X = 10;
let Y = 5;
let N = 15;
 
document.write(maximumNum(X, Y, N));
 
// This code is contributed by target_2 
 
</script>

Output: 
15

 

Time Complexity: O(1)
Auxiliary Space: O(1)

 


Article Tags :