Find the largest possible value of K such that K modulo X is Y
Last Updated :
07 Oct, 2023
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++
#include <bits/stdc++.h>
using namespace std;
int findMaxSoln( int n, int x, int y)
{
int ans = INT_MIN;
for ( int k = 0; k <= n; k++) {
if (k % x == y) {
ans = max(ans, k);
}
}
return ((ans >= 0
&& ans <= n)
? ans
: -1);
}
int main()
{
int n = 15, x = 10, y = 5;
cout << findMaxSoln(n, x, y);
return 0;
}
|
Java
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));
}
}
|
Python3
import sys
def findMaxSoln(n, x, y):
ans = - sys.maxsize
for k in range (n + 1 ):
if (k % x = = y):
ans = max (ans, k)
return (ans if (ans > = 0 and
ans < = n) else - 1 )
if __name__ = = '__main__' :
n = 15
x = 10
y = 5
print (findMaxSoln(n, x, y))
|
C#
using System;
class GFG{
static int findMaxSoln( int n,
int x, int y)
{
int ans = int .MinValue;
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;
Console.Write(findMaxSoln(n, x, y));
}
}
|
Javascript
<script>
function findMaxSoln(n, x, y)
{
var ans = -1000000000;
for ( var k = 0; k <= n; k++) {
if (k % x == y) {
ans = Math.max(ans, k);
}
}
return ((ans >= 0
&& ans <= n)
? ans
: -1);
}
var n = 15, x = 10, y = 5;
document.write( findMaxSoln(n, x, y));
</script>
|
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:
- Calculate K1 as K = N – N % X + Y and K2 as K = N – N%X + Y – X.
- If K1 lies in the range [0, N], print K1.
- Otherwise, if K2 lies in the range [0, N], print K2.
- Otherwise, print -1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findMaxSoln( int n, int x, int y)
{
if (n - n % x + y <= n) {
return n - n % x + y;
}
else {
return n - n % x - (x - y);
}
}
int main()
{
int n = 15, x = 10, y = 5;
int ans = findMaxSoln(n, x, y);
cout << ((ans >= 0 && ans <= n) ? ans : -1);
}
|
Java
import java.util.*;
class GFG{
static int findMaxSoln( int n, int x, int y)
{
if (n - n % x + y <= n)
{
return n - n % x + y;
}
else
{
return n - n % x - (x - y);
}
}
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 ));
}
}
|
Python3
def findMaxSoln(n, x, y):
if (n - n % x + y < = n):
return n - n % x + y;
else :
return n - n % x - (x - y);
if __name__ = = '__main__' :
n = 15 ;
x = 10 ;
y = 5 ;
ans = findMaxSoln(n, x, y);
print (( ans if (ans > = 0 and ans < = n) else - 1 ));
|
C#
using System;
class GFG{
static int findMaxSoln( int n,
int x, int y)
{
if (n - n % x + y <= n)
{
return n - n % x + y;
}
else
{
return n - n % x - (x - y);
}
}
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));
}
}
|
Javascript
<script>
function findMaxSoln(n , x , y) {
if (n - n % x + y <= n) {
return n - n % x + y;
}
else {
return n - n % x - (x - y);
}
}
var n = 15, x = 10, y = 5;
var ans = findMaxSoln(n, x, y);
document.write(
((ans >= 0 && ans <= n) ? ans : -1)
);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Brute Force in python:
Approach:
We can simply iterate from N to 0 and check if each number K modulo X is equal to Y. The first number we find will be the largest possible value of K.
- Define a function called largest_possible_value_of_K that takes in three integer inputs: N, X, and Y.
- Use a for loop to iterate from N down to 0. Each iteration represents a possible value of K.
- Check if K modulo X is equal to Y. If it is, return K as the largest possible value of K.
- If the for loop completes without finding a suitable value of K, return -1 to indicate that no solution was found.
C++
#include <iostream>
int largest_possible_value_of_K( int N, int X, int Y) {
for ( int K = N; K >= 0; K--) {
if (K % X == Y) {
return K;
}
}
return -1;
}
int main() {
int N = 15;
int X = 10;
int Y = 5;
std::cout << largest_possible_value_of_K(N, X, Y) << std::endl;
N = 187;
X = 10;
Y = 5;
std::cout << largest_possible_value_of_K(N, X, Y) << std::endl;
return 0;
}
|
Java
import java.util.Scanner;
public class Main {
static int largestPossibleValueOfK( int N, int X, int Y)
{
for ( int K = N; K >= 0 ; K--) {
if (K % X == Y) {
return K;
}
}
return - 1 ;
}
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
int N = 15 ;
int X = 10 ;
int Y = 5 ;
System.out.println(
largestPossibleValueOfK(N, X, Y));
N = 187 ;
X = 10 ;
Y = 5 ;
System.out.println(largestPossibleValueOfK(
N, X, Y));
scanner.close();
}
}
|
Python3
def largest_possible_value_of_K(N, X, Y):
for K in range (N, - 1 , - 1 ):
if K % X = = Y:
return K
return - 1
N = 15
X = 10
Y = 5
print (largest_possible_value_of_K(N, X, Y))
N = 187
X = 10
Y = 5
print (largest_possible_value_of_K(N, X, Y))
|
C#
using System;
class GFG
{
static int LargestPossibleValueOfK( int N, int X, int Y)
{
for ( int K = N; K >= 0; K--)
{
if (K % X == Y)
{
return K;
}
}
return -1;
}
static void Main()
{
int N = 15;
int X = 10;
int Y = 5;
Console.WriteLine(LargestPossibleValueOfK(N, X, Y));
N = 187;
X = 10;
Y = 5;
Console.WriteLine(LargestPossibleValueOfK(N, X, Y));
}
}
|
Javascript
function GFG(N, X, Y) {
for (let K = N; K >= 0; K--) {
if (K % X === Y) {
return K;
}
}
return -1;
}
function main() {
let N = 15;
let X = 10;
let Y = 5;
console.log(GFG(N, X, Y));
N = 187;
X = 10;
Y = 5;
console.log(GFG(N, X, Y));
}
main();
|
Time Complexity: O(N)
Space Complexity: O(1)
Share your thoughts in the comments
Please Login to comment...