📌  相关文章
📜  通过用距给定范围最远的互质数替换元素来修改数组

📅  最后修改于: 2021-05-17 01:53:52             🧑  作者: Mango

给定一个由N个整数和两个正整数LR组成的数组arr [] ,任务是为每个数组元素找到在[L,R]范围内最远的互质数。

例子:

方法:给定问题可以通过在每个数组元素的给定范围[L,R]上进行迭代来解决,并从数组元素中找到距离最远的GCD 1元素。请按照以下步骤解决问题:

  • 遍历给定数组arr []并执行以下步骤:
    • 初始化两个变量,例如d0coPrime-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)