Saturday, October 29, 2022
HomeWordPress DevelopmentTest if Y could be made a number of of X by...

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


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 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

  

import java.io.*;

import java.util.*;

  

class GFG {

  

    

    

    public static void test(int X, int Y)

     X <= d)

            System.out.println("Sure");

        else

            System.out.println("No");

    

  

    

    public static void important(String[] args)

    {

        int X = 7;

        int Y = 15;

  

        

        test(X, Y);

    }

}

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

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments