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

📅  最后修改于: 2021-04-23 08:16:53             🧑  作者: Mango

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

例子:

天真的方法:解决该问题的最简单方法是检查是否在K[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


输出:
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

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
输出:
15

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