Construct an array from GCDs of consecutive elements in given array
Last Updated :
18 Aug, 2022
Given an array a[] of n elements. The task is to find the array (say b[]) of n + 1 such that Greatest Common Divisor of b[i] and b[i + 1] is equals to a[i]. If multiple solutions exist, print the one whose array sum is minimum.
Examples:
Input : a[] = { 1, 2, 3 }
Output : 1 2 6 3
GCD(1, 2) = 1
GCD(2, 6) = 2
GCD(6, 3) = 3
Also, 1 + 2 + 6 + 3 = 12 which is smallest among all
possible value of array that can be constructed.
Input : a[] = { 5, 10, 5 }
Output : 5 10 10 5
Suppose there is only one number in the given array a[]. Let it be K, and then both numbers in the constructed array (say b[]) will be K and K.
So, the value of the b[0] will be a[0] only. Now consider that, we are done up to index i i.e we have already processed upto index i and calculated b[i + 1].
Now the gcd(b[i + 1], b[i + 2]) = a[i + 1] and gcd(b[i + 2], b[i + 3]) = a[i + 2]. So, b[i + 2] >= lcm(a[i + 1], a[i + 2]). Or, b[i + 2] will be multiple of lcm(a[i + 1], a[i + 2]). As we want the minimum sum, so we want the minimum value of b[i + 2]. So, b[i + 2] = lcm(a[i + 2], a[i + 3]).
Below is the implementation of this approach:
C++
#include <bits/stdc++.h>
using namespace std;
int lcm( int a, int b)
{
return (a * b) / __gcd(a, b);
}
void printArray( int a[], int n)
{
cout << a[0] << " " ;
for ( int i = 0; i < n - 1; i++)
cout << lcm(a[i], a[i + 1]) << " " ;
cout << a[n - 1] << endl;
}
int main()
{
int a[] = { 1, 2, 3 };
int n = sizeof (a) / sizeof (a[0]);
printArray(a, n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int __gcd( int a, int b)
{
if (a == 0 || b == 0 )
return 0 ;
if (a == b)
return a;
if (a > b)
return __gcd(a - b, b);
return __gcd(a, b - a);
}
static int lcm( int a, int b)
{
return (a * b) / __gcd(a, b);
}
static void printArray( int a[], int n)
{
System.out.print( a[ 0 ] + " " );
for ( int i = 0 ; i < n - 1 ; i++)
System.out.print(lcm(a[i],
a[i + 1 ]) + " " );
System.out.print(a[n - 1 ]);
}
public static void main (String[] args)
{
int a[] = { 1 , 2 , 3 };
int n = a.length;
printArray(a, n);
}
}
|
Python3
def __gcd( a, b):
if (a = = 0 or b = = 0 ):
return 0
if (a = = b):
return a
if (a > b):
return __gcd(a - b, b)
return __gcd(a, b - a)
def lcm(a, b):
return (a * b) / __gcd(a, b)
def printArray(a, n):
print ( str (a[ 0 ]) + " " )
for i in range ( 0 ,n - 1 ):
print ( str (lcm(a[i],a[i + 1 ])) + " " )
print (a[n - 1 ])
a = [ 1 , 2 , 3 ]
n = len (a)
printArray(a, n)
|
C#
using System;
class GFG {
static int __gcd( int a, int b)
{
if (a == 0 || b == 0)
return 0;
if (a == b)
return a;
if (a > b)
return __gcd(a - b, b);
return __gcd(a, b - a);
}
static int lcm( int a, int b)
{
return (a * b) / __gcd(a, b);
}
static void printArray( int []a, int n)
{
Console.Write( a[0] + " " );
for ( int i = 0; i < n - 1; i++)
Console.Write(lcm(a[i],
a[i + 1]) + " " );
Console.Write(a[n - 1]);
}
public static void Main ()
{
int []a = {1, 2, 3};
int n = a.Length;
printArray(a, n);
}
}
|
PHP
<?php
function __gcd( $a , $b )
{
if ( $a == 0 or $b == 0)
return 0 ;
if ( $a == $b )
return $a ;
if ( $a > $b )
return __gcd( $a - $b , $b ) ;
return __gcd( $a , $b - $a ) ;
}
function lcm( $a , $b )
{
return ( $a * $b ) / __gcd( $a , $b );
}
function printArray( $a , $n )
{
echo $a [0] , " " ;
for ( $i = 0; $i < $n - 1; $i ++)
echo lcm( $a [ $i ], $a [ $i + 1]) , " " ;
echo $a [ $n - 1] , "\n" ;
}
$a = array (1, 2, 3);
$n = count ( $a );
printArray( $a , $n );
?>
|
Javascript
<script>
function __gcd(a, b)
{
if (a == 0 || b == 0)
return 0;
if (a == b)
return a;
if (a > b)
return __gcd(a - b, b);
return __gcd(a, b - a);
}
function lcm(a, b)
{
return (a * b) / __gcd(a, b);
}
function printArray(a, n)
{
document.write( a[0] + " " );
for ( var i = 0; i < n - 1; i++)
document.write( lcm(a[i], a[i + 1]) + " " );
document.write( a[n - 1] + "<br>" );
}
var a = [ 1, 2, 3 ];
var n = a.length;
printArray(a, n);
</script>
|
Complexity Analysis:
- Time complexity: O(n * log(max(a, b)), where n represents the size of the given array.
- Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...