Modify array by replacing elements with their farthest co-prime number from a given range
Given an array arr[] consisting of N integers and two positive integers L and R, the task is to find the farthest co-prime number in the range [L, R] for every array element.
Examples:
Input: arr[] = {5, 150, 120}, L = 2, R = 250
Output: 249 7 247
Explanation:
The number which is co-prime with arr[0] and farthest from it is 249.
The number which is co-prime with arr[1] and farthest from it is 7.
The number which is co-prime with arr[2] and farthest from it is 247.
Input: arr[] = {60, 246, 75, 103, 155, 110}, L = 2, R = 250
Output: 60 246 75 103 155 110
Approach: The given problem can be solved by iterating over the given range [L, R] for every array element and find the farthest element from it having GCD 1 with the array element. Follow the steps below to solve the problem:
- Traverse the given array arr[] and perform the following steps:
- Initialize two variables, say d as 0 and coPrime as -1, to store the farthest distance and the number coprime with the arr[i] respectively.
- Iterate over the given range [L, R] and perform the following steps:
- Update the value of d as the absolute difference of arr[i] and j.
- If the greatest common divisor of arr[i] and j is 1 and d is less than abs(arr[i] – j), then update the value of coPrime as j.
- Update the value of arr[i] as the coPrime.
- After completing the above steps, print the array arr[] as the resultant array.
Below is the 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 update( int arr[], int n)
{
for ( int i = 0; i < n; i++) {
int d = 0;
int coPrime = -1;
for ( int j = 2; j <= 250; j++) {
if (gcd(arr[i], j) == 1
&& d < abs (arr[i] - j)) {
d = abs (arr[i] - j);
coPrime = j;
}
}
arr[i] = coPrime;
}
for ( int i = 0; i < n; i++)
cout << arr[i] << " " ;
}
int main()
{
int arr[] = { 60, 246, 75, 103, 155, 110 };
int N = sizeof (arr) / sizeof (arr[0]);
update(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
static int gcd( int a, int b)
{
if (a == 0 )
return b;
return gcd(b % a, a);
}
static void update( int arr[], int n)
{
for ( int i = 0 ; i < n; i++)
{
int d = 0 ;
int coPrime = - 1 ;
for ( int j = 2 ; j <= 250 ; j++)
{
if (gcd(arr[i], j) == 1 &&
d < Math.abs(arr[i] - j))
{
d = Math.abs(arr[i] - j);
coPrime = j;
}
}
arr[i] = coPrime;
}
for ( int i = 0 ; i < n; i++)
System.out.print(arr[i] + " " );
}
public static void main(String[] args)
{
int arr[] = { 60 , 246 , 75 , 103 , 155 , 110 };
int N = arr.length;
update(arr, N);
}
}
|
Python3
from math import gcd
def update(arr, n):
for i in range (n):
d = 0
coPrime = - 1
for j in range ( 2 , 251 , 1 ):
if (gcd(arr[i], j) = = 1 and d < abs (arr[i] - j)):
d = abs (arr[i] - j)
coPrime = j
arr[i] = coPrime
for i in range (n):
print (arr[i],end = " " )
if __name__ = = '__main__' :
arr = [ 60 , 246 , 75 , 103 , 155 , 110 ]
N = len (arr)
update(arr, N)
|
C#
using System;
class GFG {
static int gcd( int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static void update( int [] arr, int n)
{
for ( int i = 0; i < n; i++) {
int d = 0;
int coPrime = -1;
for ( int j = 2; j <= 250; j++) {
if (gcd(arr[i], j) == 1
&& d < Math.Abs(arr[i] - j)) {
d = Math.Abs(arr[i] - j);
coPrime = j;
}
}
arr[i] = coPrime;
}
for ( int i = 0; i < n; i++)
Console.Write(arr[i] + " " );
}
public static void Main( string [] args)
{
int [] arr = { 60, 246, 75, 103, 155, 110 };
int N = arr.Length;
update(arr, N);
}
}
|
Javascript
<script>
function gcd(a, b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
function update(arr, n)
{
for (let i = 0; i < n; i++)
{
let d = 0;
let coPrime = -1;
for (let j = 2; j <= 250; j++)
{
if (gcd(arr[i], j) == 1 &&
d < Math.abs(arr[i] - j))
{
d = Math.abs(arr[i] - j);
coPrime = j;
}
}
arr[i] = coPrime;
}
for (let i = 0; i < n; i++)
document.write(arr[i] + " " );
}
let arr = [ 60, 246, 75, 103, 155, 110 ];
let N = arr.length;
update(arr, N)
</script>
|
Output:
247 5 248 250 2 249
Time Complexity: O((R – L) * N)
Auxiliary Space: O(1)
Last Updated :
13 Sep, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...