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++
// C++ program for the above approachÂ
#include <bits/stdc++.h>using namespace std;Â
// Function to calculate GCD// of the integers a and bint 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 elementvoid 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 Codeint 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 approachimport java.io.*;import java.lang.*;import java.util.*;Â
class GFG{Â
// Function to calculate GCD// of the integers a and bstatic 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 elementstatic 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 Codepublic 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 approachfrom math import gcdÂ
# Function to find the farthest# co-prime number over the range# [L, R] for each array elementdef 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 Codeif __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 approachusing 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
<script>Â
// JavaScript program to implement// the above approachÂ
// Function to calculate GCD// of the integers a and bfunction gcd(a, 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 elementfunction update(arr, n){          // Traverse the array arr[]    for(let i = 0; i < n; i++)    {                  // Stores the distance        // between j and arr[i]        let d = 0;          // Stores the integer coprime        // which is coprime is arr[i]        let coPrime = -1;          // Traverse the range [2, 250]        for(let 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(let i = 0; i < n; i++)        document.write(arr[i] + " ");}Â
Â
// Driver codeÂ
         let arr = [ 60, 246, 75, 103, 155, 110 ];    let N = arr.length;          update(arr, N)      </script> |
247 5 248 250 2 249
Â
Time Complexity: O((R – L) * N)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



