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 <iostream> 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 = Z void 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 Code int main() { int A = 5; int B = 3; findTriplet(A, B); return 0; } |
Java
// Java program for the above approach import 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 = Z def 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 Code A = 5 B = 3 findTriplet(A, B) # This code is contributed by Saurabh Jaiswal |
C#
// C# program of the above approach using 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
<script> // JavaScript 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 = Z function findTriplet(A, B) { // If the value of B is 1 if (B == 1) { document.write(-1); return ; } // Print Answer document.write(A + " " + A * B + " " + A * (B + 1)); } // Driver Code let A = 5; let B = 3; findTriplet(A, B); // This code is contributed by Potta Lokesh </script> |
5 15 20
Time Complexity: O(1)
Auxiliary Space: O(1)
Please Login to comment...