Open In App

Generate a sequence X from given sequence Y such that Yi = gcd(X1, X2 , … , Xi)

Improve
Improve
Like Article
Like
Save
Share
Report

Given a sequence Y of size N where: 

Yi = gcd(X1, X2, X3, . . ., Xi ) of some sequence X. 

The task is to find such a sequence X if any such X is possible.

Note: If X exists there can be multiple value possible for X. Generating any one is sufficient.

Examples: 

Input: N = 2, Y = [4, 2]
Output: [4, 2]
Explanation: Y0 = gcd(X0) = X0 = 4. And gcd(4, 2) = 2 = Y1.

Input: [1, 3]
Output: -1
Explanation: No such sequence can be formed.

 

Approach: The problem can be solved based on the following observation:

  • ith element of Y = gcd(X1, X2, X3, . . ., Xi ) and (i+1)th element of Y = gcd(X1, X2, X3, . . ., Xi, Xi+1).
  • So (i+1)th element of Y can be written as gcd(gcd(X1, X2, X3, . . ., Xi ), Xi+1) ——-(1) i.e, gcd(a, b, c) = gcd( gcd(a, b), c ).
  • So (i+1)th element of Y is gcd( ith element of Y, X(i+1)) from equation 1.
  • Therefore (i+1)th element of Y must be factor of ith element of Y, as gcd of two elements is divisor of both the elements.
  • Since (i+1)th element of Y divides ith element of Y, gcd( ith element, (i+1)th element) is always equals to (i+1)th element.

Follow the illustration below:

Illustration:

For example: N = 2, Y = {4, 2}

  • X[0] = Y[0] = 4 because any number is GCD of itself.
  • Y[1] = GCD(X[0], X[1]). Now GCD(X[0]) = Y[0]. So Y[1] = GCD(Y[0], X[1]). Therefore Y[1] should be a factor of Y[0].
  • In the given example 2 is a factor of 4. So forming a sequence X is possible and X[1] can be same as Y[1]. 
    Assigning X[1] = 2 satisfies GCD (X[0, X[1]) = Y[1] = 2.
  • So the final sequence X is {4, 2}.

So follow the steps mentioned below to solve the problem based on the above observation: 

  1. Start traversing Y from the starting of the sequence.
  2. If in the given Sequence Y has any (i+1)th element which does not divide ith element then sequence X can’t be generated.
  3. If the (i+1)th element divides ith element then there exists a sequence X and it can be found by above conclusion of(i+1)th element of Y 
    is gcd( ith element of Y, Xi+1) and Xi+1 = Yi+1 is a possible value for Xi+1.

Below is the implementation of the approach:

C++




// C++ Program to implement above approach
#include <bits/stdc++.h>
using namespace std;
 
// Euclidean theorem to find gcd
int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
 
// Function to check if it is possible
// to generate lost sequence X
void checkforX(int Y[], int N)
{
    int cur_gcd = Y[0];
    bool Xexist = true;
 
    // Loop to check existence of X
    for (int i = 1; i < N; i++) {
        cur_gcd = gcd(cur_gcd, Y[i]);
        if (cur_gcd != Y[i]) {
            Xexist = false;
            break;
        }
    }
 
    // Sequence X is found
    if (Xexist) {
 
        // Loop to generate X
        for (int i = 0; i < N; i++)
            cout << Y[i] << ' ';
    }
 
    // Sequence X can't be generated
    else {
        cout << -1 << endl;
    }
}
 
// Driver code
int main()
{
    int N = 2;
 
    // Sequence Y of size 2
    int Y[] = { 4, 2 };
    checkforX(Y, N);
    return 0;
}


C




// C program to implement above approach
#include <stdio.h>
 
// Euclidean theorem to calculate gcd
int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
 
// Function to check if it is possible
// to generate lost sequence X
void checkforX(int Y[], int N)
{
    int cur_gcd = Y[0];
    int Xexist = 1;
 
    // Loop to check existence of X
    for (int i = 1; i < N; i++) {
        cur_gcd = gcd(cur_gcd, Y[i]);
        if (cur_gcd != Y[i]) {
            Xexist = 0;
            break;
        }
    }
 
    // Sequence X is found
    if (Xexist) {
 
        // Loop to generate X
        for (int i = 0; i < N; i++)
            printf("%d ", Y[i]);
    }
 
    // Sequence X can't be generated
    else {
        printf("-1");
    }
}
 
// Driver code
int main()
{
    int N = 2;
 
    // Sequence Y of size 2
    int Y[] = { 4, 2 };
    checkforX(Y, N);
    return 0;
}


Java




// Java Program to implement above approach
import java.util.*;
 
class GFG{
 
// Euclidean theorem to find gcd
static int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
 
// Function to check if it is possible
// to generate lost sequence X
static void checkforX(int Y[], int N)
{
    int cur_gcd = Y[0];
    boolean Xexist = true;
 
    // Loop to check existence of X
    for (int i = 1; i < N; i++) {
        cur_gcd = gcd(cur_gcd, Y[i]);
        if (cur_gcd != Y[i]) {
            Xexist = false;
            break;
        }
    }
 
    // Sequence X is found
    if (Xexist) {
 
        // Loop to generate X
        for (int i = 0; i < N; i++)
            System.out.print(Y[i] +" ");
    }
 
    // Sequence X can't be generated
    else {
        System.out.print(-1 +"\n");
    }
}
 
// Driver code
public static void main(String[] args)
{
    int N = 2;
 
    // Sequence Y of size 2
    int Y[] = { 4, 2 };
    checkforX(Y, N);
}
}
 
// This code is contributed by 29AjayKumar


Python3




# Python code for the above approach
 
# Euclidean theorem to find gcd
def gcd(a, b):
    if (b == 0):
        return a
    return gcd(b, a % b)
 
# Function to check if it is possible
# to generate lost sequence X
def checkforX(Y, N):
    cur_gcd = Y[0]
    Xexist = True
 
    # Loop to check existence of X
    for i in range(1, N):
        cur_gcd = gcd(cur_gcd, Y[i])
        if (cur_gcd != Y[i]):
            Xexist = False
            break
 
    # Sequence X is found
    if (Xexist):
 
        # Loop to generate X
        for i in range(N):
            print(Y[i], end=' ')
 
    # Sequence X can't be generated
    else:
        print(-1)
 
# Driver code
N = 2
 
# Sequence Y of size 2
Y = [4, 2]
checkforX(Y, N)
 
# This code is contributed by gfgking


C#




// C# Program to implement above approach
using System;
class GFG
{
   
// Euclidean theorem to find gcd
static int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
 
// Function to check if it is possible
// to generate lost sequence X
static void checkforX(int []Y, int N)
{
    int cur_gcd = Y[0];
    bool Xexist = true;
 
    // Loop to check existence of X
    for (int i = 1; i < N; i++) {
        cur_gcd = gcd(cur_gcd, Y[i]);
        if (cur_gcd != Y[i]) {
            Xexist = false;
            break;
        }
    }
 
    // Sequence X is found
    if (Xexist) {
 
        // Loop to generate X
        for (int i = 0; i < N; i++)
            Console.Write(Y[i] + " ");
    }
 
    // Sequence X can't be generated
    else {
        Console.Write(-1);
    }
}
 
// Driver code
public static void Main()
{
    int N = 2;
 
    // Sequence Y of size 2
    int []Y = { 4, 2 };
    checkforX(Y, N);
}
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript




<script>
        // JavaScript code for the above approach
 
        // Euclidean theorem to find gcd
        function gcd(a, b) {
            if (b == 0)
                return a;
            return gcd(b, a % b);
        }
 
        // Function to check if it is possible
        // to generate lost sequence X
        function checkforX(Y, N) {
            let cur_gcd = Y[0];
            let Xexist = true;
 
            // Loop to check existence of X
            for (let i = 1; i < N; i++) {
                cur_gcd = gcd(cur_gcd, Y[i]);
                if (cur_gcd != Y[i]) {
                    Xexist = false;
                    break;
                }
            }
 
            // Sequence X is found
            if (Xexist) {
 
                // Loop to generate X
                for (let i = 0; i < N; i++)
                    document.write(Y[i] + ' ');
            }
 
            // Sequence X can't be generated
            else {
                document.write(-1 + '<br>');
            }
        }
 
        // Driver code
        let N = 2;
 
        // Sequence Y of size 2
        let Y = [4, 2];
        checkforX(Y, N);
 
  // This code is contributed by Potta Lokesh
    </script>


Output

4 2 

Time Complexity: O(N)
Auxiliary space: O(1)



Last Updated : 22 Dec, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads