Verify if Y could be made a number of of X by including any worth to each

[ad_1]

View Dialogue

Enhance Article

Save Article

Like Article

View Dialogue

Enhance Article

Save Article

Like Article

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:

  • 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 strategy.

Java

  

import java.io.*;

import java.util.*;

  

class GFG {

  

    

    

    public static void test(int X, int Y)

    

  

    

    public static void principal(String[] args)

    {

        int X = 7;

        int Y = 15;

  

        

        test(X, Y);

    }

}

Time Complexity: O(1) 
Auxiliary House: O(1)

[ad_2]

Leave a Reply