Skip to content
Related Articles

Related Articles

Improve Article

Find the largest possible value of K such that K modulo X is Y

  • Last Updated : 28 Apr, 2021

Given three integers N, X, and Y, the task is to find out the largest positive integer K such that K % X = Y where 0 ≤ K ≤ N. Print -1 if no such K exists.

Examples:

Input: N = 15, X = 10, Y = 5
Output:15
Explanation:
As 15 % 10 = 5

Input: N = 187, X = 10, Y = 5
Output: 185

Naive Approach: The simplest approach to solve the problem is to check for each possible value of K in the range [0, N], whether the condition K % X = Y is satisfied or not. If no such K exists, print -1. Otherwise, print the largest possible value of K from the range that satisfied the condition.



Below is the implementation of the above approach:

C++




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

Java




// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to find the largest
// positive integer K such that K % x = y
static int findMaxSoln(int n, int x, int y)
{
     
    // Stores the minimum solution
    int ans = Integer.MIN_VALUE;
 
    for(int k = 0; k <= n; k++)
    {
        if (k % x == y)
        {
            ans = Math.max(ans, k);
        }
    }
 
    // Return the maximum possible value
    return ((ans >= 0 && ans <= n) ?
             ans : -1);
}
 
// Driver Code
public static void main(String[] args)
{
    int n = 15, x = 10, y = 5;
     
    System.out.print(findMaxSoln(n, x, y));
}
}
 
// This code is contributed by Amit Katiyar

Python3




# Python3 program for the above approach
import sys
  
# Function to find the largest
# positive integer K such that
# K % x = y
def findMaxSoln(n, x, y):
     
    # Stores the minimum solution
    ans = -sys.maxsize
  
    for k in range(n + 1):
        if (k % x == y):
            ans = max(ans, k)
  
    # Return the maximum possible value
    return (ans if (ans >= 0 and
                    ans <= n) else -1)
                     
# Driver Code
if __name__ == '__main__':
     
    n = 15
    x = 10
    y = 5
  
    print(findMaxSoln(n, x, y))
  
# This code is contributed by Amit Katiyar

C#




// C# program for the above approach
using System;
class GFG{
 
// Function to find the largest
// positive integer K such that
// K % x = y
static int findMaxSoln(int n,
                       int x, int y)
{   
  // Stores the minimum solution
  int ans = int.MinValue;
 
  for(int k = 0; k <= n; k++)
  {
    if (k % x == y)
    {
      ans = Math.Max(ans, k);
    }
  }
 
  // Return the maximum possible value
  return ((ans >= 0 && ans <= n) ?
           ans : -1);
}
 
// Driver Code
public static void Main(String[] args)
{
  int n = 15, x = 10, y = 5;   
  Console.Write(findMaxSoln(n, x, y));
}
}
 
// This code is contributed by shikhasingrajput

Javascript




<script>
 
// Javascript program for the above approach
 
// Function to find the largest
// positive integer K such that K % x = y
function findMaxSoln(n, x, y)
{
 
    // Stores the minimum solution
    var ans = -1000000000;
 
    for (var k = 0; k <= n; k++) {
        if (k % x == y) {
            ans = Math.max(ans, k);
        }
    }
 
    // Return the maximum possible value
    return ((ans >= 0
             && ans <= n)
                ? ans
                : -1);
}
 
// Driver Code
var n = 15, x = 10, y = 5;
document.write( findMaxSoln(n, x, y));
 
// This code is contributed by rrrtnx.
</script>
Output: 
15

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

Efficient Approach: The above approach can be optimized based on the observation that K can obtain one of the following values to satisfy the equation:

K = N – N % X + Y 
or 
K = N – N % X + Y – X

Follow the steps below to solve the problem:

  1. Calculate K1 as K = N – N % X + Y and K2 as K = N – N%X + Y – X.
  2. If K1 lies in the range [0, N], print K1.
  3. Otherwise, if K2 lies in the range [0, N], print K2.
  4. Otherwise, print -1.

Below is the implementation of the above approach:

C++




// C++ Program to implement
// the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the largest positive
// integer K such that K % x = y
int findMaxSoln(int n, int x, int y)
{
    // Possible value of K as K1
    if (n - n % x + y <= n) {
        return n - n % x + y;
    }
 
    // Possible value of K as K2
    else {
        return n - n % x - (x - y);
    }
}
 
// Driver Code
int main()
{
    int n = 15, x = 10, y = 5;
    int ans = findMaxSoln(n, x, y);
    cout << ((ans >= 0 && ans <= n) ? ans : -1);
}

Java




// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
 
// Function to find the largest positive
// integer K such that K % x = y
static int findMaxSoln(int n, int x, int y)
{
     
    // Possible value of K as K1
    if (n - n % x + y <= n)
    {
        return n - n % x + y;
    }
 
    // Possible value of K as K2
    else
    {
        return n - n % x - (x - y);
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int n = 15, x = 10, y = 5;
    int ans = findMaxSoln(n, x, y);
     
    System.out.print(((ans >= 0 &&
                       ans <= n) ? ans : -1));
}
}
 
// This code is contributed by Amit Katiyar

Python3




# Python3 program to implement
# the above approach
 
# Function to find the largest positive
# integer K such that K % x = y
def findMaxSoln(n, x, y):
   
    # Possible value of K as K1
    if (n - n % x + y <= n):
        return n - n % x + y;
 
    # Possible value of K as K2
    else:
        return n - n % x - (x - y);
 
# Driver Code
if __name__ == '__main__':
    n = 15;
    x = 10;
    y = 5;
    ans = findMaxSoln(n, x, y);
    print(( ans if (ans >= 0 and ans <= n) else -1));
 
# This code is contributed by 29AjayKumar

C#




// C# program to implement
// the above approach
using System;
class GFG{
 
// Function to find the largest
// positive integer K such that
// K % x = y
static int findMaxSoln(int n,
                       int x, int y)
{
  // Possible value of K as K1
  if (n - n % x + y <= n)
  {
    return n - n % x + y;
  }
 
  // Possible value of K as K2
  else
  {
    return n - n % x - (x - y);
  }
}
 
// Driver Code
public static void Main(String[] args)
{
  int n = 15, x = 10, y = 5;
  int ans = findMaxSoln(n, x, y);
  Console.Write(((ans >= 0 &&
                  ans <= n) ?
                  ans : -1));
}
}
 
// This code is contributed by shikhasingrajput

Javascript




<script>
 
// Javascript program to implement
// the above approach
 
    // Function to find the largest positive
    // integer K such that K % x = y
    function findMaxSoln(n , x , y) {
 
        // Possible value of K as K1
        if (n - n % x + y <= n) {
            return n - n % x + y;
        }
 
        // Possible value of K as K2
        else {
            return n - n % x - (x - y);
        }
    }
 
    // Driver Code
     
        var n = 15, x = 10, y = 5;
        var ans = findMaxSoln(n, x, y);
 
        document.write(
        ((ans >= 0 && ans <= n) ? ans : -1)
        );
 
// This code contributed by umadevi9616
 
</script>
Output: 
15

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

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :