Find a triplet (X, Y, Z) such that all are divisible by A, exactly one is divisible by both A and B, and X + Y = Z
Given two integers A and B, the task is to find a triplet (X, Y, Z) such that all of them are divisible by A, exactly one of them is divisible by both A and B, and X + Y = Z.
Input: A = 5, B = 3
Output: 10 50 60
Explanation: For the triplet (10, 50, 60), all of them are divisible by 5, 60 is divisible by both 5 and 3, and 10 + 50 = 60. Therefore, (10, 50, 60) is valid triplet. Other possible triplets are (5, 25, 30), (5, 15, 20)
Input: A = 7, B = 11
Output: 28 154 182
Approach: The given problem is an observation-based problem that can be solved using basic mathematics. It can be observed that the triplet (A, A * B, A * (B + 1)), satisfies all of the given conditions except when the value of B is 1. In that case, it can be seen that the condition that exactly one of them should be divisible by both A and B will always be violated. So, if B = 1, no valid triplet exists, otherwise print (A, A * B, A * (B + 1)).
Below is the implementation of the above approach:
5 15 20
Time Complexity: O(1)
Auxiliary Space: O(1)