VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-two-numbers-a-and-b-of-x-and-y-digits-respectively-with-gcd-having-z-digits/

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


  • Courses
  • Tutorials
  • Interview Prep

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

Last Updated : 23 Jul, 2025

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:


Output
A = 11 B = 100

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


 

Comment
Article Tags: