📌  相关文章
📜  找到 K 的最大可能值,使得 K 模 X 为 Y

📅  最后修改于: 2021-10-25 10:33:02             🧑  作者: Mango

给定三个整数NXY ,任务是找出最大的正整数K使得K % X = Y其中0 ≤ K ≤ N 。如果不存在这样的K,则打印-1

例子:

朴素方法:解决问题的最简单方法是检查[0, N]范围内K 的每个可能值,是否满足条件K % X = Y。如果不存在这样的K ,则打印-1 。否则,从满足条件的范围中打印K的最大可能值。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
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


C++
// C++ Program to implement
// the above approach
 
#include 
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


输出:
15

时间复杂度: O(N)
辅助空间: O(1)

高效方法:基于观察到K可以获得以下值之一来满足方程,可以优化上述方法:

请按照以下步骤解决问题:

  1. K1计算为K = N – N % X + Y ,将K2 计算K = N – N%X + Y – X。
  2. 如果K1位于[0, N]范围内,则打印K1
  3. 否则,如果K2位于[0, N]范围内,则打印K2
  4. 否则,打印-1

下面是上述方法的实现:

C++

// C++ Program to implement
// the above approach
 
#include 
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

蟒蛇3

# 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


输出:
15

时间复杂度: O(1)
辅助空间: O(1)