给定一个由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
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