Skip to content
Related Articles

Related Articles

Find two numbers A and B of X and Y digits respectively with GCD having Z digits

View Discussion
Improve Article
Save Article
  • Last Updated : 09 Feb, 2022
View Discussion
Improve Article
Save Article

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++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the desired numbers
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;
}
 
// Driver Code
int main()
{
    int X = 2, Y = 3, Z = 1;
    findTwoNumbers(X, Y, Z);
    return 0;
}


Java




// Java program for the above approach
class GFG {
 
  // Function to find the desired numbers
  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);
  }
 
  // Driver Code
  public static void main(String args[]) {
    int X = 2, Y = 3, Z = 1;
    findTwoNumbers(X, Y, Z);
  }
}
 
// This code is contributed by Saurabh Jaiswal


Python3




# python3 program for the above approach
 
# Function to find the desired numbers
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}")
 
# Driver Code
if __name__ == "__main__":
 
    X, Y, Z = 2, 3, 1
    findTwoNumbers(X, Y, Z)
 
    # This code is contributed by rakeshsahni


C#




// C# program for the above approach
using System;
public class GFG
{
   
// Function to find the desired numbers
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);
}
 
// Driver Code
public static void Main()
{
    int X = 2, Y = 3, Z = 1;
    findTwoNumbers(X, Y, Z);
}
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript




<script>
    // JavaScript code for the above approach
 
 
    // Function to find the desired numbers
    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);
    }
 
    // Driver Code
 
    let X = 2, Y = 3, Z = 1;
    findTwoNumbers(X, Y, Z);
 
   // This code is contributed by Potta Lokesh
</script>


 
 

Output

A = 11 B = 100

 

Time Complexity: O(max(X, Y))
Auxiliary Space: O(1)

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!