Check if an Array can be converted to other by replacing pairs with GCD
Given arrays A[] and B[] each of length N and A[i] has all the elements from 1 to N, the task is to check if it is possible to convert A[] to B[] by replacing any pair of A[] with their GCD.
Examples:
Input: N = 2, A[] = {1, 2}, B[] = {1, 1}
Output: YES
Explanation:
First Operation – For A[], choose i = 0 and j = 1. GCD(A[0], A[1]) = GCD(1, 2) = 1. Replace both the elements with GCD = 1. So A[] = {1, 1}.
Input: N = 3, A[] = {1, 2, 3}, B[] = {1, 2, 2}
Output: NO
Explanation: It can be verify that it’s not possible to convert A[] to B[] by using given operation.
Approach: Implement the idea below to solve the problem
The problem is observation based and can be solved by calculating GCD of A[i] and B[i] for each index. It should be noted that if B[i]>A[i] at any index, then it is impossible to change A[i] as B[i].
So, This idea gives us approach if GCD(A[i], B[i]) = B[i], Then it is possible to convert A[i] to B[i] at that index, else not possible.
Follow the steps mentioned below to implement the idea:
- Make a boolean variable flag and initialize it to true.
- Run a loop from i = 0 to N-1:
- If GCD(A[i], B[i])=B[i] then continue the loop.
- Else mark the flag as false and break the loop.
- If the flag is true then output YES else NO.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int GCD( int a, int b)
{
return b == 0 ? a : GCD(b, a % b);
}
bool conversionChecker( int N, int B[])
{
bool flag = true ;
for ( int i = 0; i < N; i++) {
if (GCD((i + 1), B[i]) == B[i]) {
continue ;
}
else {
flag = false ;
break ;
}
}
return flag;
}
int main() {
int N = 4;
int B1[] = { 1, 2, 3, 2 };
cout<<(conversionChecker(N, B1) ? "YES" : "NO" )<<endl;
N = 5;
int B2[] = { 2, 4, 5, 6, 7 };
cout<<(conversionChecker(N, B2) ? "YES" : "NO" )<<endl;
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
int N = 4 ;
int B1[] = { 1 , 2 , 3 , 2 };
System.out.println(conversionChecker(N, B1) ? "YES"
: "NO" );
N = 5 ;
int B2[] = { 2 , 4 , 5 , 6 , 7 };
System.out.println(conversionChecker(N, B2) ? "YES"
: "NO" );
;
}
static boolean conversionChecker( int N, int B[])
{
boolean flag = true ;
for ( int i = 0 ; i < N; i++) {
if (GCD((i + 1 ), B[i]) == B[i]) {
continue ;
}
else {
flag = false ;
break ;
}
}
return flag;
}
static int GCD( int a, int b)
{
return b == 0 ? a : GCD(b, a % b);
}
}
|
Python3
def GCD(a, b):
return a if b = = 0 else GCD(b, a % b)
def conversionChecker(N, B):
flag = True
for i in range (N):
if (GCD((i + 1 ), B[i]) = = B[i]):
continue
else :
flag = False
break
return flag
N = 4
B1 = [ 1 , 2 , 3 , 2 ]
print ( "YES" if conversionChecker(N, B1) else "NO" )
N = 5
B2 = [ 2 , 4 , 5 , 6 , 7 ]
print ( "YES" if conversionChecker(N, B2) else "NO" )
|
C#
using System;
public class GFG {
static public void Main()
{
int N = 4;
int [] B1 = { 1, 2, 3, 2 };
Console.WriteLine(conversionChecker(N, B1) ? "YES"
: "NO" );
N = 5;
int [] B2 = { 2, 4, 5, 6, 7 };
Console.WriteLine(conversionChecker(N, B2) ? "YES"
: "NO" );
}
static bool conversionChecker( int N, int [] B)
{
bool flag = true ;
for ( int i = 0; i < N; i++) {
if (GCD((i + 1), B[i]) == B[i]) {
continue ;
}
else {
flag = false ;
break ;
}
}
return flag;
}
static int GCD( int a, int b)
{
return b == 0 ? a : GCD(b, a % b);
}
}
|
Javascript
function GCD(a, b)
{
return b == 0 ? a : GCD(b, a % b);
}
function conversionChecker(N, B)
{
let flag = true ;
for (let i = 0; i < N; i++) {
if (GCD((i + 1), B[i]) == B[i]) {
continue ;
}
else {
flag = false ;
break ;
}
}
return flag;
}
let N = 4;
let B1 = [1, 2, 3, 2 ];
console.log(conversionChecker(N, B1) ? "YES" : "NO" );
N = 5;
let B2 = [2, 4, 5, 6, 7 ];
console.log(conversionChecker(N, B2) ? "YES" : "NO" );
|
Time Complexity: O(N * logM), Where M is the max element of B[].
Auxiliary Space: O(1)
Efficient Approach:
In this approach, We don’t need to calculate GCD at each index, Which will save a little bit of time. For A[i] and B[i], There are 3 possible cases. Which are discussed below:
- If B[i] = A[i]: In such indices, We don’t need to perform the operation, Because A[i] is already equal to B[i].
- If B[i] < A[i]: In this case, A[i] can be converted to B[i] only and only if A[i] % B[i] = 0. Formally B[i] is a perfect divisor of A[i].
- If B[i] > A[i]: In this case, it is impossible to convert A[i] as B[i].
Follow the steps mentioned below to implement the idea:
- Make a boolean variable flag and initialize it to true.
- Run a loop from i = 0 to N-1:
- If A[i] and B[i] are equal, then continue the loop.
- Else if B[i] < A[i], then continue only if B[i] perfectly divides A[i].
- Else mark flag as false and break the loop.
- 3. If flag is true output YES else NO.
Code to implement the approach:
C++
#include <cstring>
#include <iostream>
using namespace std;
bool conversionChecker( int N, int B[])
{
bool flag = true ;
for ( int i = 0; i < N; i++) {
if (B[i] == (i + 1)) {
continue ;
}
else if (B[i] < (i + 1) && (i + 1) % B[i] == 0) {
continue ;
}
else {
flag = false ;
break ;
}
}
return flag;
}
int GCD( int a, int b) { return b == 0 ? a : GCD(b, a % b); }
int main()
{
int N = 4;
int B1[] = { 1, 2, 3, 2 };
cout << (conversionChecker(N, B1) ? "YES" : "NO" )
<< endl;
N = 6;
int B2[] = { 4, 6, 9, 1, 0, 5 };
cout << (conversionChecker(N, B2) ? "YES" : "NO" )
<< endl;
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
int N = 4 ;
int B1[] = { 1 , 2 , 3 , 2 };
System.out.println(conversionChecker(N, B1) ? "YES"
: "NO" );
N = 6 ;
int B2[] = { 4 , 6 , 9 , 1 , 0 , 5 };
System.out.println(conversionChecker(N, B2) ? "YES"
: "NO" );
}
static boolean conversionChecker( int N, int B[])
{
boolean flag = true ;
for ( int i = 0 ; i < N; i++) {
if (B[i] == (i + 1 )) {
continue ;
}
else if (B[i] < (i + 1 )
&& (i + 1 ) % B[i] == 0 ) {
continue ;
}
else {
flag = false ;
break ;
}
}
return flag;
}
static int GCD( int a, int b)
{
return b == 0 ? a : GCD(b, a % b);
}
}
|
Python3
def conversionChecker(N, B):
flag = True
for i in range (N):
if B[i] = = (i + 1 ):
continue
elif (B[i] < (i + 1 ) and (i + 1 ) % B[i] = = 0 ):
continue
else :
flag = False
break
return flag
def GCD(a, b):
return a if b = = 0 else GCD(b, a % b)
N = 4
B1 = [ 1 , 2 , 3 , 2 ]
if (conversionChecker(N, B1) is True ):
print ( "YES" )
else :
print ( "NO" )
N = 6 ;
B2 = [ 4 , 6 , 9 , 1 , 0 , 5 ];
if (conversionChecker(N, B2) is True ):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
class GFG {
public static void Main( string [] args)
{
int N = 4;
int [] B1 = { 1, 2, 3, 2 };
Console.WriteLine(conversionChecker(N, B1) ? "YES"
: "NO" );
N = 6;
int [] B2 = { 4, 6, 9, 1, 0, 5 };
Console.WriteLine(conversionChecker(N, B2) ? "YES"
: "NO" );
}
static bool conversionChecker( int N, int [] B)
{
bool flag = true ;
for ( int i = 0; i < N; i++) {
if (B[i] == (i + 1)) {
continue ;
}
else if (B[i] < (i + 1)
&& (i + 1) % B[i] == 0) {
continue ;
}
else {
flag = false ;
break ;
}
}
return flag;
}
static int GCD( int a, int b)
{
return b == 0 ? a : GCD(b, a % b);
}
}
|
Javascript
function conversionChecker(N, B) {
let flag = true ;
for (let i = 0; i < N; i++) {
if (B[i] == (i + 1)) {
continue ;
}
else if (B[i] < (i + 1)
&& (i + 1) % B[i] == 0) {
continue ;
}
else {
flag = false ;
break ;
}
}
return flag;
}
function GCD(a, b) {
return b == 0 ? a : GCD(b, a % b);
}
let N = 4;
let B1 = [1, 2, 3, 2];
console.log(conversionChecker(N, B1) ? "YES"
: "NO" );
console.log('<br>')
N = 6;
let B2 = [4, 6, 9, 1, 0, 5];
console.log(conversionChecker(N, B2) ? "YES"
: "NO" );
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Related Articles:
Last Updated :
14 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...