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
Last Updated :
09 Feb, 2022
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++
#include <iostream>
using namespace std;
void findTriplet( int A, int B)
{
if (B == 1) {
cout << -1;
return ;
}
cout << A << " " << A * B
<< " " << A * (B + 1);
}
int main()
{
int A = 5;
int B = 3;
findTriplet(A, B);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
static void findTriplet( int A, int B)
{
if (B == 1 ) {
System.out.println(- 1 );
return ;
}
System.out.println(A + " " + A * B + " " + A * (B + 1 ));
}
public static void main (String[] args) {
int A = 5 ;
int B = 3 ;
findTriplet(A, B);
}
}
|
Python3
def findTriplet(A, B):
if (B = = 1 ):
print ( - 1 )
return
print (f "{A} {A * B} {A * (B + 1)}" )
A = 5
B = 3
findTriplet(A, B)
|
C#
using System;
class GFG
{
static void findTriplet( int A, int B)
{
if (B == 1) {
Console.Write(-1);
return ;
}
Console.Write(A + " " + A * B + " " + A * (B + 1));
}
public static int Main()
{
int A = 5;
int B = 3;
findTriplet(A, B);
return 0;
}
}
|
Javascript
<script>
function findTriplet(A, B)
{
if (B == 1) {
document.write(-1);
return ;
}
document.write(A + " " + A * B
+ " " + A * (B + 1));
}
let A = 5;
let B = 3;
findTriplet(A, B);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...