Thursday, September 21, 2023
HomeSoftware DevelopmentVerify if A and B could be lowered to 0 by decrementing...

Verify if A and B could be lowered to 0 by decrementing with x and y with absolute distinction at most Okay


Geek Week

Given three integers A, B, and K. The task is to check whether A and B can be reduced to zero by decrementing x and y from A and B respectively such that abs(x – y) ≤ K.


Input: A = 2, B = 7, K = 3
Output: YES
Explanation: Decrement values in the following way:

  • Decrement 1 from A and 4 from B such that abs(1 – 4) ≤ 3, therefore, current value of A = 1 and B = 3.
  • Decrement 1 from A and 3 from B such that abs(1 – 3) ≤ 3, current value of A = 0 and B = 0.

So, it is possible to reduce both the numbers to 0.

Input: A = 9, B = 8, K = 0
Output: NO


Approach: The task can be solved with a simple observation. The idea is to find the minimum and maximum out of A and B. If the minimum number multiplied by (1+K) is less than the maximum, then it is not possible to convert A and B to zero, else they can be converted to zero.

Below is the implementation of the above approach:


#include <bits/stdc++.h>

using namespace std;


bool isPossibleToReduce(int A, int B, int k)




    int mn = min(A, B);

    int mx = max(A, B);





    if (mn * (1 + k) < mx) {

        return false;




    return true;



int main()


    int A = 2, B = 7;

    int K = 3;


    if (isPossibleToReduce(A, B, K))

        cout << "YES";


        cout << "NO";


    return 0;




def isPossibleToReduce(A, B, k):




    mn = min(A, B)

    mx = max(A, B)





    if (mn * (1 + k) < mx):

        return False



    return True


if __name__ == "__main__":


    A = 2

    B = 7

    K = 3


    if (isPossibleToReduce(A, B, K)):









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.




Please enter your comment!
Please enter your name here

Most Popular

Recent Comments