给定一个由N个整数和两个正整数L和R组成的数组arr [] ,任务是为每个数组元素找到在[L,R]范围内最远的互质数。
例子:
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
方法:给定问题可以通过在每个数组元素的给定范围[L,R]上进行迭代来解决,并从数组元素中找到距离最远的GCD 1元素。请按照以下步骤解决问题:
- 遍历给定数组arr []并执行以下步骤:
- 初始化两个变量,例如d为0和coPrime为-1 ,以分别存储最远距离和数量与arr [i]互素的数。
- 遍历给定范围[L,R]并执行以下步骤:
- 将d的值更新为arr [i]和j的绝对差。
- 如果arr [i]和j的最大公约数为1,并且d小于abs(arr [i] – j) ,则将coPrime的值更新为j 。
- 将arr [i]的值更新为coPrime 。
- 完成上述步骤后,将数组arr []打印为结果数组。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to calculate GCD
// of the integers a and b
int gcd(int a, int b)
{
// Base Case
if (a == 0)
return b;
// Recursively find the GCD
return gcd(b % a, a);
}
// Function to find the farthest
// co-prime number over the range
// [L, R] for each array element
void update(int arr[], int n)
{
// Traverse the array arr[]
for (int i = 0; i < n; i++) {
// Stores the distance
// between j and arr[i]
int d = 0;
// Stores the integer coprime
// which is coprime is arr[i]
int coPrime = -1;
// Traverse the range [2, 250]
for (int j = 2; j <= 250; j++) {
// If gcd of arr[i] and j is 1
if (gcd(arr[i], j) == 1
&& d < abs(arr[i] - j)) {
// Update the value of d
d = abs(arr[i] - j);
// Update the value
// of coPrime
coPrime = j;
}
}
// Update the value of arr[i]
arr[i] = coPrime;
}
// Print the array arr[]
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
}
// Driver Code
int main()
{
int arr[] = { 60, 246, 75, 103, 155, 110 };
int N = sizeof(arr) / sizeof(arr[0]);
update(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
// Function to calculate GCD
// of the integers a and b
static int gcd(int a, int b)
{
// Base Case
if (a == 0)
return b;
// Recursively find the GCD
return gcd(b % a, a);
}
// Function to find the farthest
// co-prime number over the range
// [L, R] for each array element
static void update(int arr[], int n)
{
// Traverse the array arr[]
for(int i = 0; i < n; i++)
{
// Stores the distance
// between j and arr[i]
int d = 0;
// Stores the integer coprime
// which is coprime is arr[i]
int coPrime = -1;
// Traverse the range [2, 250]
for(int j = 2; j <= 250; j++)
{
// If gcd of arr[i] and j is 1
if (gcd(arr[i], j) == 1 &&
d < Math.abs(arr[i] - j))
{
// Update the value of d
d = Math.abs(arr[i] - j);
// Update the value
// of coPrime
coPrime = j;
}
}
// Update the value of arr[i]
arr[i] = coPrime;
}
// Print the array arr[]
for(int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 60, 246, 75, 103, 155, 110 };
int N = arr.length;
update(arr, N);
}
}
// This code is contributed by Kingash
输出:
247 5 248 250 2 249
时间复杂度: O((R – L)* N)
辅助空间: O(1)