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:
- Start traversing Y from the starting of the sequence.
- 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.
- 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++
#include <bits/stdc++.h>
using namespace std;
int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
void checkforX( int Y[], int N)
{
int cur_gcd = Y[0];
bool Xexist = true ;
for ( int i = 1; i < N; i++) {
cur_gcd = gcd(cur_gcd, Y[i]);
if (cur_gcd != Y[i]) {
Xexist = false ;
break ;
}
}
if (Xexist) {
for ( int i = 0; i < N; i++)
cout << Y[i] << ' ' ;
}
else {
cout << -1 << endl;
}
}
int main()
{
int N = 2;
int Y[] = { 4, 2 };
checkforX(Y, N);
return 0;
}
|
C
#include <stdio.h>
int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
void checkforX( int Y[], int N)
{
int cur_gcd = Y[0];
int Xexist = 1;
for ( int i = 1; i < N; i++) {
cur_gcd = gcd(cur_gcd, Y[i]);
if (cur_gcd != Y[i]) {
Xexist = 0;
break ;
}
}
if (Xexist) {
for ( int i = 0; i < N; i++)
printf ( "%d " , Y[i]);
}
else {
printf ( "-1" );
}
}
int main()
{
int N = 2;
int Y[] = { 4, 2 };
checkforX(Y, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int gcd( int a, int b)
{
if (b == 0 )
return a;
return gcd(b, a % b);
}
static void checkforX( int Y[], int N)
{
int cur_gcd = Y[ 0 ];
boolean Xexist = true ;
for ( int i = 1 ; i < N; i++) {
cur_gcd = gcd(cur_gcd, Y[i]);
if (cur_gcd != Y[i]) {
Xexist = false ;
break ;
}
}
if (Xexist) {
for ( int i = 0 ; i < N; i++)
System.out.print(Y[i] + " " );
}
else {
System.out.print(- 1 + "\n" );
}
}
public static void main(String[] args)
{
int N = 2 ;
int Y[] = { 4 , 2 };
checkforX(Y, N);
}
}
|
Python3
def gcd(a, b):
if (b = = 0 ):
return a
return gcd(b, a % b)
def checkforX(Y, N):
cur_gcd = Y[ 0 ]
Xexist = True
for i in range ( 1 , N):
cur_gcd = gcd(cur_gcd, Y[i])
if (cur_gcd ! = Y[i]):
Xexist = False
break
if (Xexist):
for i in range (N):
print (Y[i], end = ' ' )
else :
print ( - 1 )
N = 2
Y = [ 4 , 2 ]
checkforX(Y, N)
|
C#
using System;
class GFG
{
static int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static void checkforX( int []Y, int N)
{
int cur_gcd = Y[0];
bool Xexist = true ;
for ( int i = 1; i < N; i++) {
cur_gcd = gcd(cur_gcd, Y[i]);
if (cur_gcd != Y[i]) {
Xexist = false ;
break ;
}
}
if (Xexist) {
for ( int i = 0; i < N; i++)
Console.Write(Y[i] + " " );
}
else {
Console.Write(-1);
}
}
public static void Main()
{
int N = 2;
int []Y = { 4, 2 };
checkforX(Y, N);
}
}
|
Javascript
<script>
function gcd(a, b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
function checkforX(Y, N) {
let cur_gcd = Y[0];
let Xexist = true ;
for (let i = 1; i < N; i++) {
cur_gcd = gcd(cur_gcd, Y[i]);
if (cur_gcd != Y[i]) {
Xexist = false ;
break ;
}
}
if (Xexist) {
for (let i = 0; i < N; i++)
document.write(Y[i] + ' ' );
}
else {
document.write(-1 + '<br>');
}
}
let N = 2;
let Y = [4, 2];
checkforX(Y, N);
</script>
|
Time Complexity: O(N)
Auxiliary space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
22 Dec, 2021
Like Article
Save Article