热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

通过用给定范围内最远的同质数替换元素来修改数组

通过用给定范围内最远的同质数替换元素来修改数组原文:https:

通过用给定范围内最远的同质数替换元素来修改数组

原文:https://www . geeksforgeeks . org/通过用给定范围内最远的同素数替换元素来修改数组/

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

示例:

输入: arr[] = {5,150,120},L = 2,R = 250
输出: 249 7 247
说明:
与 arr[0]同素且离它最远的数是 249。
与 arr[1]同素且离它最远的数是 7。
与 arr[2]同素且离它最远的数是 247。

输入: arr[] = {60,246,75,103,155,110},L = 2,R = 250
输出: 60 246 75 103 155 110

方法:给定的问题可以通过对每个数组元素迭代给定的范围【L,R】并找到与该数组元素具有 GCD 1 的最远元素来解决。按照以下步骤解决问题:


  • 遍历给定数组 arr[] ,并执行以下步骤:

    • 初始化两个变量,说 d0互素-1 ,分别用arr【I】存储最远距离和互素数。

    • 迭代给定的范围【L,R】并执行以下步骤:

      • d 的值更新为arr【I】j 的绝对差值。

      • 如果arr[I]j的最大公约数为1d小于ABS(arr[I]–j),则将互质的值更新为 j



    • arr[i] 的值更新为互质



  • 完成上述步骤后,打印数组 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         // 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                 // 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         cout <}
// 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     {
        // 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             {
                // 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         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

Python 3

# 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                 # 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             // 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                     // 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             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.

java 描述语言


Output: 

247 5 248 250 2 249

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


推荐阅读
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 闭包一直是Java社区中争论不断的话题,很多语言都支持闭包这个语言特性,闭包定义了一个依赖于外部环境的自由变量的函数,这个函数能够访问外部环境的变量。本文以JavaScript的一个闭包为例,介绍了闭包的定义和特性。 ... [详细]
  • 本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ... [详细]
  • 本文介绍了Python高级网络编程及TCP/IP协议簇的OSI七层模型。首先简单介绍了七层模型的各层及其封装解封装过程。然后讨论了程序开发中涉及到的网络通信内容,主要包括TCP协议、UDP协议和IPV4协议。最后还介绍了socket编程、聊天socket实现、远程执行命令、上传文件、socketserver及其源码分析等相关内容。 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • 原文地址:https:www.cnblogs.combaoyipSpringBoot_YML.html1.在springboot中,有两种配置文件,一种 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • javascript  – 概述在Firefox上无法正常工作
    我试图提出一些自定义大纲,以达到一些Web可访问性建议.但我不能用Firefox制作.这就是它在Chrome上的外观:而那个图标实际上是一个锚点.在Firefox上,它只概述了整个 ... [详细]
  • 不同优化算法的比较分析及实验验证
    本文介绍了神经网络优化中常用的优化方法,包括学习率调整和梯度估计修正,并通过实验验证了不同优化算法的效果。实验结果表明,Adam算法在综合考虑学习率调整和梯度估计修正方面表现较好。该研究对于优化神经网络的训练过程具有指导意义。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • Python瓦片图下载、合并、绘图、标记的代码示例
    本文提供了Python瓦片图下载、合并、绘图、标记的代码示例,包括下载代码、多线程下载、图像处理等功能。通过参考geoserver,使用PIL、cv2、numpy、gdal、osr等库实现了瓦片图的下载、合并、绘图和标记功能。代码示例详细介绍了各个功能的实现方法,供读者参考使用。 ... [详细]
  • Android源码深入理解JNI技术的概述和应用
    本文介绍了Android源码中的JNI技术,包括概述和应用。JNI是Java Native Interface的缩写,是一种技术,可以实现Java程序调用Native语言写的函数,以及Native程序调用Java层的函数。在Android平台上,JNI充当了连接Java世界和Native世界的桥梁。本文通过分析Android源码中的相关文件和位置,深入探讨了JNI技术在Android开发中的重要性和应用场景。 ... [详细]
  • C++字符字符串处理及字符集编码方案
    本文介绍了C++中字符字符串处理的问题,并详细解释了字符集编码方案,包括UNICODE、Windows apps采用的UTF-16编码、ASCII、SBCS和DBCS编码方案。同时说明了ANSI C标准和Windows中的字符/字符串数据类型实现。文章还提到了在编译时需要定义UNICODE宏以支持unicode编码,否则将使用windows code page编译。最后,给出了相关的头文件和数据类型定义。 ... [详细]
  • 本文介绍了机器学习手册中关于日期和时区操作的重要性以及其在实际应用中的作用。文章以一个故事为背景,描述了学童们面对老先生的教导时的反应,以及上官如在这个过程中的表现。同时,文章也提到了顾慎为对上官如的恨意以及他们之间的矛盾源于早年的结局。最后,文章强调了日期和时区操作在机器学习中的重要性,并指出了其在实际应用中的作用和意义。 ... [详细]
author-avatar
herogan
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有