Find two numbers A and B of X and Y digits respectively with GCD having Z digits
Last Updated :
12 Dec, 2022
Given three positive integers X, Y, and Z. The task is to find two numbers A and B of X and Y digits respectively with their GCD having Z digits. where Z ≤ min(X, Y). If there are multiple possible answers print any of them.
Examples:
Input: X = 2, Y = 3, Z = 1
Output: A = 11, B = 100
Explanation: A and B contains 2 and 3 digits respectively and GCD(A, B) is 1 which has 1 digit.
Input: X = 2, Y = 2, Z = 2
Output: A = 13, B = 26
Approach: The task can be solved using some observations. Calculate A equals 10x-1, B as 10y-1, and C as 10z-1. Increment A with C to achieve GCD with Z digits. Follow the below steps to solve the problem:
- Initialize 3 variables A, B, C with 1.
- Append Z -1 zeroes with C.
- Append X – 1 zeroes in last of A.
- Append Y – 1 zero in last of B.
- Increment A by the value of C.
- Print A and B as the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findTwoNumbers( int X, int Y, int Z)
{
int A, B, C;
A = B = C = 1;
for ( int i = 1; i <= X - 1; i++) {
A = A * 10;
}
for ( int i = 1; i <= Y - 1; i++) {
B = B * 10;
}
for ( int i = 1; i <= Z - 1; i++) {
C = C * 10;
}
A = A + C;
cout << "A = " << A << " B = " << B;
}
int main()
{
int X = 2, Y = 3, Z = 1;
findTwoNumbers(X, Y, Z);
return 0;
}
|
Java
import java.io.*;
public class GFG {
static void findTwoNumbers( int X, int Y, int Z) {
int A, B, C;
A = B = C = 1 ;
for ( int i = 1 ; i <= X - 1 ; i++) {
A = A * 10 ;
}
for ( int i = 1 ; i <= Y - 1 ; i++) {
B = B * 10 ;
}
for ( int i = 1 ; i <= Z - 1 ; i++) {
C = C * 10 ;
}
A = A + C;
System.out.println( "A = " + A + " B = " + B);
}
public static void main(String args[]) {
int X = 2 , Y = 3 , Z = 1 ;
findTwoNumbers(X, Y, Z);
}
}
|
Python3
def findTwoNumbers(X, Y, Z):
A = B = C = 1
for i in range ( 1 , X):
A = A * 10
for i in range ( 1 , Y):
B = B * 10
for i in range ( 1 , Z):
C = C * 10
A = A + C
print (f "A = {A} B = {B}" )
if __name__ = = "__main__" :
X, Y, Z = 2 , 3 , 1
findTwoNumbers(X, Y, Z)
|
C#
using System;
public class GFG
{
static void findTwoNumbers( int X, int Y, int Z)
{
int A, B, C;
A = B = C = 1;
for ( int i = 1; i <= X - 1; i++) {
A = A * 10;
}
for ( int i = 1; i <= Y - 1; i++) {
B = B * 10;
}
for ( int i = 1; i <= Z - 1; i++) {
C = C * 10;
}
A = A + C;
Console.Write( "A = " + A + " B = " + B);
}
public static void Main()
{
int X = 2, Y = 3, Z = 1;
findTwoNumbers(X, Y, Z);
}
}
|
Javascript
<script>
function findTwoNumbers(X, Y, Z) {
let A, B, C;
A = B = C = 1;
for (let i = 1; i <= X - 1; i++) {
A = A * 10;
}
for (let i = 1; i <= Y - 1; i++) {
B = B * 10;
}
for (let i = 1; i <= Z - 1; i++) {
C = C * 10;
}
A = A + C;
document.write( "A = " + A + " B = " + B);
}
let X = 2, Y = 3, Z = 1;
findTwoNumbers(X, Y, Z);
</script>
|
Time Complexity: O(max(X, Y))
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...