Find original Array from given Array of GCD of prefix
Last Updated :
06 Jan, 2023
Given an array B[] of length N, the task is to print an array A[] such that for every ith element B[i] is gcd of the first i elements of A[] i.e. Bi = gcd (A1, A2, …., Ai) and if no such array A[] exists, print ?1.
Examples:
Input: B[] = {4, 2}
Output: 4 2
Explanation: One possible answer is [4, 2] because
B can be generated as follows: B=[gcd(4), gcd(4, 2)]=[4, 2].
Input: B[] = {2, 6, 8, 10}
Output: -1
Explanation: No array exists which satisfies the given condition.
Approach: The problem can be solved based on the following observation:
- We know that Bi = gcd(A1, A2, . . . , Ai) and Bi+1 = gcd(A1, A2, . . ., Ai, Ai+1) = gcd(gcd(A1, A2, . . ., Ai), Ai+1) = gcd(Bi, Ai+1). This way, we can write Bi+1 = gcd(Bi , Ai+1).
- The implication of this is that Bi+1 must be a factor of Bi , Since gcd of two numbers is divisor of both numbers. Hence, condition Bi+1 divides Bi should hold for all 1 ? i <N.
- So if the given array has any such i where Bi+1 doesn’t divide Bi , no such A can exist.
- The given array B is a valid candidate for A as Bi+1 = gcd(Bi, Ai+1), but we have Ai+1 = Bi+1 . Since Bi+1 divide Bi , gcd(Bi, Bi+1) = Bi+1. So the given array B satisfies our condition and can be printed as array A.
Follow the below steps to solve the problem:
- Initialize a boolean variable flag = true.
- Iterate on the given array and check the following:
- If the next element is not a factor of the current element:
- Set flag = false.
- Terminate the loop.
- If the flag is true:
- Print the given array B[].
- else print -1.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void findOriginal( int arr[], int n)
{
bool flag = true ;
for ( int i = 0; i < n - 1; i++) {
if (arr[i] % arr[i + 1] != 0) {
flag = false ;
break ;
}
}
if (flag == false )
cout << "-1" ;;
if (flag == true )
for ( int i =0;i<n;i++) {
cout << arr[i] << " " ;
}
}
int main()
{
int B[] = { 4, 2 };
int N = sizeof (B) / sizeof (B[0]);
findOriginal(B, N);
return 0;
}
|
Java
class GFG {
static void findOriginal( int arr[], int n)
{
boolean flag = true ;
for ( int i = 0 ; i < n - 1 ; i++) {
if (arr[i] % arr[i + 1 ] != 0 ) {
flag = false ;
break ;
}
}
if (flag == false )
System.out.println(- 1 );
else {
for ( int val : arr) {
System.out.print(val + " " );
}
}
}
public static void main(String[] args)
{
int B[] = { 4 , 2 };
int N = B.length;
findOriginal(B, N);
}
}
|
C#
using System;
public class GFG {
static void findOriginal( int []arr, int n)
{
bool flag = true ;
for ( int i = 0; i < n - 1; i++) {
if (arr[i] % arr[i + 1] != 0) {
flag = false ;
break ;
}
}
if (flag == false )
Console.WriteLine(-1);
else {
foreach ( int val in arr) {
Console.Write(val + " " );
}
}
}
public static void Main(String[] args)
{
int []B = { 4, 2 };
int N = B.Length;
findOriginal(B, N);
}
}
|
Python3
def findOriginal(arr, n):
flag = True
i = 0
for i in range (n - 1 ):
if (arr[i] % arr[i + 1 ] ! = 0 ):
flag = False
break
else :
i + = 1
if (flag = = False ):
print ( - 1 )
else :
for val in arr:
print (val , end = " " )
B = [ 4 , 2 ]
N = len (B)
findOriginal(B, N)
|
Javascript
<script>
function findOriginal(arr, n)
{
let flag = true ;
for (let i = 0; i < n - 1; i++) {
if (arr[i] % arr[i + 1] != 0) {
flag = false ;
break ;
}
}
if (flag == false )
document.write(-1);
else {
for (let val in arr) {
document.write(arr[val] + " " );
}
}
}
let B = [ 4, 2 ];
let N = B.length;
findOriginal(B, N);
</script>
|
Time Complexity: O(N) for traversing the given array.
Auxiliary Space: O(1) as constant space is used.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...