Given two integers P and Q, the duty is to verify whether or not a pair (P, Q) is equal or not, and a pair is claimed to be equal if there exist some optimistic integers X and Y such that PX = QY.
Examples:
Enter: P = 16 , Q = 4
Output: Sure
Clarification: 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 may be solved based mostly on the next commentary:
For PX = QY to be true for some integer pair (X, Y), any one of many beneath circumstances have to be true:
- There should exist some integer Ok, such that
- X = Y = 0
Now to implement this, beneath algorithm can be utilized:
- Discover most(max) and minimal(min) quantity for 2 integer.
- Iterate a loop and verify 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 shouldn’t be equal.
Under is the implementation of the above method.
Java
|
Time Complexity: O(N)
Auxiliary Area: O(1)