📌  相关文章
📜  检查 A 和 B 是否可以通过 x 和 y 减少到 0,最大绝对差为 K

📅  最后修改于: 2022-05-13 01:56:06.415000             🧑  作者: Mango

检查 A 和 B 是否可以通过 x 和 y 减少到 0,最大绝对差为 K

给定三个整数ABK 。任务是检查AB是否可以通过分别从 A 和 B 递减xy减少到,使得abs(x – y) ≤ K

例子:

方法:这个任务可以通过一个简单的观察来解决。这个想法是找到 最小值和最大值 出A和B。如果最小数乘以( 1+K )小于最大值,不能将A和B转换为零,否则可以将它们转换为零。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if it is possible
// to reduce A and B to zero
bool isPossibleToReduce(int A, int B, int k)
{
    // Finding minimum and maximum
    // of A and B
    int mn = min(A, B);
    int mx = max(A, B);
 
    // If minimum multiply by (1+k)
    // is less than maximum then
    // return false
    if (mn * (1 + k) < mx) {
        return false;
    }
 
    // Else return true;
    return true;
}
 
// Driver Code
int main()
{
    int A = 2, B = 7;
    int K = 3;
 
    if (isPossibleToReduce(A, B, K))
        cout << "YES";
    else
        cout << "NO";
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG {
 
    /// Function to check if it is possible
    // to reduce A and B to zero
    static boolean isPossibleToReduce(int A, int B, int k)
    {
       
        // Finding minimum and maximum
        // of A and B
        int mn = Math.min(A, B);
        int mx = Math.max(A, B);
 
        // If minimum multiply by (1+k)
        // is less than maximum then
        // return false
        if (mn * (1 + k) < mx) {
            return false;
        }
 
        // Else return true;
        return true;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int A = 2, B = 7;
        int K = 3;
 
        if (isPossibleToReduce(A, B, K))
            System.out.println("YES");
        else
            System.out.println("NO");
    }
}
 
// This code is contributed by dwivediyash


Python3
# python program for the above approach
 
# Function to check if it is possible
# to reduce A and B to zero
def isPossibleToReduce(A, B, k):
 
    # Finding minimum and maximum
    # of A and B
    mn = min(A, B)
    mx = max(A, B)
 
    # If minimum multiply by (1+k)
    # is less than maximum then
    # return false
    if (mn * (1 + k) < mx):
        return False
 
    # Else return true;
    return True
 
# Driver Code
if __name__ == "__main__":
 
    A = 2
    B = 7
    K = 3
 
    if (isPossibleToReduce(A, B, K)):
        print("YES")
 
    else:
        print("NO")
 
    # This code is contributed by rakeshsahni


C#
// C# program for the above approach
using System;
 
public class GFG {
 
    /// Function to check if it is possible
    // to reduce A and B to zero
    static bool isPossibleToReduce(int A, int B, int k)
    {
       
        // Finding minimum and maximum
        // of A and B
        int mn = Math.Min(A, B);
        int mx = Math.Max(A, B);
 
        // If minimum multiply by (1+k)
        // is less than maximum then
        // return false
        if (mn * (1 + k) < mx) {
            return false;
        }
 
        // Else return true;
        return true;
    }
 
    // Driver Code
    public static void Main(string[] args)
    {
        int A = 2, B = 7;
        int K = 3;
 
        if (isPossibleToReduce(A, B, K))
            Console.WriteLine("YES");
        else
            Console.WriteLine("NO");
    }
}
 
// This code is contributed by AnkThon


Javascript



输出
YES

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