Thursday, October 6, 2022
HomeWordPress DevelopmentVerify if there exists some constructive integers X and Y such that...

Verify if there exists some constructive integers X and Y such that P^X is the same as Q^Y


View Dialogue

Enhance Article

Save Article

Like Article

View Dialogue

Enhance Article

Save Article

Like Article

Given two integers P and Q, the duty is to examine whether or not a pair (P, Q) is equal or not, and a pair is claimed to be equal if there exist some constructive integers X and Y such that PX = QY.

Examples:

Enter: P = 16 , Q = 4
Output: Sure

Rationalization: Let X = 2 and Y = 4. Thus, PX = 162 = 256 and QY = 44 = 256 . Thus, the pair (16,4) is equal.

Enter: P = 12 , Q = 24
Output: No

Strategy: The issue will be solved primarily based on the next remark:

For PX = QY to be true for some integer pair (X, Y),  any one of many under instances should be true:

  1. There should exist some integer Okay, such that 
  2. X = Y = 0

Now to implement this, under algorithm can be utilized:

  • Discover most(max) and minimal(min) quantity for 2 integer.
  • Iterate a loop and examine if max and min is equal or max is divisible by min, then pair of integer is equal and break from the loop.
  • In any other case, pair of integer will not be equal.

 Under is the implementation of the above strategy.

Java

  

import java.io.*;

import java.util.*;

  

public class GFG {

  

    

    

    public static void examine(int p, int q)

    {

        int max = Math.max(p, q);

        int min = Math.min(p, q);

        whereas (true) {

            if (max == min) {

                System.out.println("Sure");

                break;

            }

            if (max % min != 0) {

                System.out.println("No");

                break;

            }

            int temp = max / min;

            max = Math.max(temp, min);

            min = Math.min(temp, min);

        }

    }

  

    

    public static void foremost(String[] args)

    {

        int P = 16;

        int Q = 4;

  

        

        examine(P, Q);

    }

}

Time Complexity: O(N)
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