Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
[ad_1]
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 search out whether or not it’s doable to make X a a number of of Y.
Examples:
Enter: X = 7, Y = 15
Output: Sure
Clarification: 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
Strategy: The issue could be solved based mostly on the next concept:
- First, if X = Y, then the reply is clearly “Sure”.
- In any other case, word that irrespective of which Z we select, the distinction between X and Y stays fixed.
Let d = Y – X. A sound Z exists if and provided that X ≤ d.
Comply with the steps talked about beneath to implement the above concept:
Beneath is the implementation of the above strategy.
|
Time Complexity: O(1)
Auxiliary House: O(1)
[ad_2]