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

📅  最后修改于: 2021-09-06 05:19:02             🧑  作者: 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


Python3
# python 3 program for the above approach
from math import gcd
 
# Function to find the farthest
# co-prime number over the range
# [L, R] for each array element
def update(arr, n):
   
    # Traverse the array arr[]
    for i in range(n):
       
        # Stores the distance
        # between j and arr[i]
        d = 0
 
        # Stores the integer coprime
        # which is coprime is arr[i]
        coPrime = -1
 
        # Traverse the range [2, 250]
        for j in range(2, 251, 1):
           
            # If gcd of arr[i] and j is 1
            if (gcd(arr[i], j) == 1 and 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 i in range(n):
        print(arr[i],end =" ")
 
# Driver Code
if __name__ == '__main__':
    arr = [60, 246, 75, 103, 155, 110]
    N = len(arr)
    update(arr, N)
     
    # This code is contributed by ipg2016107.


C#
// C# program for the above approach
using System;
 
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++)
            Console.Write(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 ukasp.


Javascript


输出:
247 5 248 250 2 249

时间复杂度: O((R – L) * N)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live