Modify sequence of first N natural numbers to a given array by replacing pairs with their GCD
Last Updated :
06 Apr, 2023
Given an integer N and an array arr[], the task is to check if a sequence of first N natural numbers, i.e. {1, 2, 3, .. N} can be made equal to arr[] by choosing any pair (i, j) from the sequence and replacing both i and j by GCD of i and j. If possible, then print “Yes”. Otherwise, print “No”.
Examples:
Input: N = 4, arr[] = {1, 2, 3, 2}
Output: Yes
Explanation: For the pair (2, 4) in the sequence {1, 2, 3, 4}, GCD(2, 4) = 2. Now, the sequence modifies to {1, 2, 3, 2}, which is same as arr[].
Input: N = 3, arr[] = {1, 2, 2}
Output: No
Approach:
We can start by iterating over all pairs of numbers (i, j) such that 1 ? i < j ? N and finding their GCD using the Euclidean algorithm. Then, if we find a pair (i, j) such that GCD(i, j) is equal to arr[i-1] and arr[j-1], we can replace both i and j with GCD(i, j) and continue with the modified sequence. If we are able to modify the sequence to be equal to arr[], then we can print “Yes”. Otherwise, we can print “No”.
Algorithm:
- Initialize a sequence of first N natural numbers, i.e. {1, 2, 3, .. N}.
- Iterate over all pairs of numbers (i, j) such that 1 ? i < j ? N.
- Compute the GCD of i and j using the Euclidean algorithm.
- If GCD(i, j) is equal to arr[i-1] and arr[j-1], replace both i and j with GCD(i, j) in the sequence.
- If the modified sequence is equal to arr[], print “Yes” and return.
- If no such pair (i, j) is found, print “No” and return.
Implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int gcd( int a, int b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
void checkSequence( int N, int arr[]) {
vector< int > seq(N);
for ( int i = 0; i < N; i++)
seq[i] = i+1;
for ( int i = 0; i < N; i++) {
for ( int j = i+1; j < N; j++) {
int g = gcd(seq[i], seq[j]);
if (g == arr[i] && g == arr[j]) {
seq[i] = seq[j] = g;
break ;
}
}
if (seq == vector< int >(arr, arr+N)) {
cout << "Yes\n" ;
return ;
}
}
cout << "No\n" ;
}
int main() {
int N = 4;
int arr[] = {1, 2, 3, 2};
checkSequence(N, arr);
return 0;
}
|
Java
import java.util.*;
public class Main {
public static int gcd( int a, int b)
{
if (a == 0 )
return b;
return gcd(b % a, a);
}
public static void checkSequence( int N, int [] arr)
{
List<Integer> seq = new ArrayList<>();
for ( int i = 0 ; i < N; i++)
seq.add(i + 1 );
for ( int i = 0 ; i < N; i++) {
for ( int j = i + 1 ; j < N; j++) {
int g = gcd(seq.get(i), seq.get(j));
if (g == arr[i] && g == arr[j]) {
seq.set(i, g);
seq.set(j, g);
break ;
}
}
if (seq.equals(Arrays.asList(
Arrays.stream(arr).boxed().toArray(
Integer[] :: new )))) {
System.out.println( "Yes" );
return ;
}
}
System.out.println( "No" );
}
public static void main(String[] args)
{
int N = 4 ;
int [] arr = { 1 , 2 , 3 , 2 };
checkSequence(N, arr);
}
}
|
Python3
from typing import List
import math
def gcd(a: int , b: int ) - > int :
if a = = 0 :
return b
return gcd(b % a, a)
def checkSequence(N: int , arr: List [ int ]):
seq = list ( range ( 1 , N + 1 ))
for i in range (N):
for j in range (i + 1 , N):
g = gcd(seq[i], seq[j])
if g = = arr[i] and g = = arr[j]:
seq[i] = g
seq[j] = g
break
if seq = = list ( range ( 1 , N + 1 )):
print ( "Yes" )
return
print ( "No" )
if __name__ = = '__main__' :
N = 4
arr = [ 1 , 2 , 3 , 2 ]
checkSequence(N, arr)
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class MainClass {
public static int gcd( int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
public static void checkSequence( int N, int [] arr)
{
List< int > seq = new List< int >();
for ( int i = 0; i < N; i++)
seq.Add(i + 1);
for ( int i = 0; i < N; i++) {
for ( int j = i + 1; j < N; j++) {
int g = gcd(seq[i], seq[j]);
if (g == arr[i] && g == arr[j]) {
seq[i] = g;
seq[j] = g;
break ;
}
}
if (seq.SequenceEqual(arr)) {
Console.WriteLine( "Yes" );
return ;
}
}
Console.WriteLine( "No" );
}
public static void Main( string [] args)
{
int N = 4;
int [] arr = { 1, 2, 3, 2 };
checkSequence(N, arr);
}
}
|
Javascript
function gcd(a, b) {
if (a === 0) {
return b;
}
return gcd(b % a, a);
}
function checkSequence(N, arr) {
const seq = Array.from({ length: N }, (_, i) => i + 1);
for (let i = 0; i < N; i++) {
for (let j = i + 1; j < N; j++) {
const g = gcd(seq[i], seq[j]);
if (g === arr[i] && g === arr[j]) {
seq[i] = g;
seq[j] = g;
break ;
}
}
if (seq.every((value, index) => value === index + 1)) {
console.log( "Yes" );
return ;
}
}
console.log( "No" );
}
const N = 4;
const arr = [1, 2, 3, 2];
checkSequence(N, arr);
|
Time Complexity: O(N * sqrt(N) * log(N))
Space Complexity: O(N)
Approach: The idea is based on the fact that the GCD of two numbers lies between 1 and the minimum of the two numbers. By definition of gcd, it’s the greatest number that divides both. Therefore, make the number at an index smaller if and only if there exists some number which is its factor. Hence, it can be concluded that for every ith index in the array, if the follow condition holds true, the array arr[] can be obtained from the sequence of first N natural numbers.
(i + 1) % arr[i] == 0
Follow the steps below to solve the problem:
- Traverse the array arr[] using variable i.
- For every ith index, check if (i + 1) % arr[i] is equal to 0 or not. If found to be false for any array element, print “No”.
- Otherwise, after complete traversal of the array, print “Yes”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void isSequenceValid(vector< int >& B,
int N)
{
for ( int i = 0; i < N; i++) {
if ((i + 1) % B[i] != 0) {
cout << "No" ;
return ;
}
}
cout << "Yes" ;
}
int main()
{
int N = 4;
vector< int > arr{ 1, 2, 3, 2 };
isSequenceValid(arr, N);
return 0;
}
|
Java
class GFG{
static void isSequenceValid( int [] B,
int N)
{
for ( int i = 0 ; i < N; i++)
{
if ((i + 1 ) % B[i] != 0 )
{
System.out.print( "No" );
return ;
}
}
System.out.print( "Yes" );
}
public static void main(String[] args)
{
int N = 4 ;
int [] arr = { 1 , 2 , 3 , 2 };
isSequenceValid(arr, N);
}
}
|
Python3
def isSequenceValid(B, N):
for i in range (N):
if ((i + 1 ) % B[i] ! = 0 ):
print ( "No" )
return
print ( "Yes" )
N = 4
arr = [ 1 , 2 , 3 , 2 ]
isSequenceValid(arr, N)
|
C#
using System;
class GFG{
static void isSequenceValid( int [] B,
int N)
{
for ( int i = 0; i < N; i++)
{
if ((i + 1) % B[i] != 0)
{
Console.WriteLine( "No" );
return ;
}
}
Console.WriteLine( "Yes" );
}
public static void Main()
{
int N = 4;
int [] arr = { 1, 2, 3, 2 };
isSequenceValid(arr, N);
}
}
|
Javascript
<script>
function isSequenceValid(B, N)
{
for (let i = 0; i < N; i++)
{
if ((i + 1) % B[i] != 0)
{
document.write( "No" );
return ;
}
}
document.write( "Yes" );
}
let N = 4;
let arr = [ 1, 2, 3, 2 ];
isSequenceValid(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...