Given two numbers X and Y (X ≤ Y), you possibly can choose any constructive integer Z and add them to each X and Y. The duty is to seek out whether or not it’s doable to make X a a number of of Y.
Examples:
Enter: X = 7, Y = 15
Output: Sure
Rationalization: We will select Z = 1 and add them to 7 and 15. Thus, 7 + 1 = 8 is an element of 15 + 1 = 16.Enter: X = 9, Y = 10
Output: No
Method: The issue could be solved primarily based on the next concept:
- First, if X = Y, then the reply is clearly “Sure”.
- In any other case, notice that irrespective of which Z we select, the distinction between X and Y stays fixed.
Let d = Y – X. A legitimate Z exists if and provided that X ≤ d.
Observe the steps talked about under to implement the above concept:
- First, we discover the distinction between X and Y let d = Y – X.
- If d = 0 or X ≤ d, then print “Sure”
- Else print “No”
Beneath is the implementation of the above method.
Java
|
Time Complexity: O(1)
Auxiliary House: O(1)