GeeksforGeeks App
Open App
Browser
Continue

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

Example:

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:

## C++

 // C++ program of the above approach#include using namespace std; // Function 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 = Zvoid findTriplet(int A, int B){    // If the value of B is 1    if (B == 1) {        cout << -1;        return;    }     // Print Answer    cout << A << " " << A * B         << " " << A * (B + 1);} // Driver Codeint main(){    int A = 5;    int B = 3;    findTriplet(A, B);     return 0;}

## Java

 // Java program for the above approachimport java.io.*;import java.lang.*;import java.util.*; class GFG {   // Function 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  static void findTriplet(int A, int B)  {     // If the value of B is 1    if (B == 1) {      System.out.println(-1);      return;    }     // Print Answer    System.out.println(A + " " + A * B + " " +  A * (B + 1));  }   // Driver Code  public static void main (String[] args) {    int A = 5;    int B = 3;    findTriplet(A, B);  }} // This code is contributed by hrithikgarg03188.

## Python3

 # Python code for the above approach # Function 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 = Zdef findTriplet(A, B):     # If the value of B is 1    if (B == 1):        print(-1)        return     # Print Answer    print(f"{A} {A * B} {A * (B + 1)}") # Driver CodeA = 5B = 3findTriplet(A, B) # This code is contributed by Saurabh Jaiswal

## C#

 // C# program of the above approachusing System;class GFG{   // Function 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  static void findTriplet(int A, int B)  {     // If the value of B is 1    if (B == 1) {      Console.Write(-1);      return;    }     // Print Answer    Console.Write(A + " " + A * B + " " + A * (B + 1));  }   // Driver Code  public static int Main()  {    int A = 5;    int B = 3;    findTriplet(A, B);    return 0;  }} // This code is contributed by Taranpreet

## Javascript



Output

5 15 20

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

My Personal Notes arrow_drop_up