📌  相关文章
📜  最大化跳跃大小以从给定起点到达所有给定点

📅  最后修改于: 2022-05-13 01:56:07.200000             🧑  作者: Mango

最大化跳跃大小以从给定起点到达所有给定点

给定一个数组, arr[]由表示数轴坐标的N个整数和一个整数S组成。如果起始位置是S ,则找到至少访问所有坐标一次所需的最大跳转大小。此外,允许双向跳跃。

例子:

方法:阅读问题后可以观察到,直线上两个相邻点之间所有差异的最大公约数是访问所有点所需的最大跳转大小。因此,要解决此问题,请按照以下步骤操作:

  1. 在数组arr中插入S ,然后对其进行排序。
  2. 求数组arr中所有相邻对的差之间的 gcd。
  3. 返回最终的 gcd 作为此问题的答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the maximum size of jump
// to reach all the coordinates
int minJumpSize(vector& arr, int S)
{
 
    // Pushing S into the line
    arr.push_back(S);
 
    // Sorting arr, to get coordinates in
    // a sorted way
    sort(arr.begin(), arr.end());
 
    // Finding gcd between the differences of
    // all adjacent pairs
    int gcd = arr[1] - arr[0];
    for (int i = 1; i < arr.size(); i++) {
        gcd = __gcd(gcd,
                    __gcd(gcd,
                          arr[i] - arr[i - 1]));
    }
 
    return gcd;
}
 
// Driver Code
int main()
{
    vector arr = { 1, 7, 3, 9, 5, 11 };
    int S = 3;
 
    cout << minJumpSize(arr, S);
}


Java
// Java program for the above approach
import java.util.ArrayList;
import java.util.Collections;
 
class GFG {
 
    public static int __gcd(int a, int b) {
        if (b == 0)
            return a;
        return __gcd(b, a % b);
    }
 
    // Function to find the maximum size of jump
    // to reach all the coordinates
    public static int minJumpSize(int[] t_arr, int S) {
        ArrayList arr = new ArrayList();
 
        for (int x : t_arr) {
            arr.add(x);
        }
 
        // Pushing S into the line
        arr.add(S);
 
        // Sorting arr, to get coordinates in
        // a sorted way
        Collections.sort(arr);
 
        // Finding gcd between the differences of
        // all adjacent pairs
        int gcd = arr.get(1) - arr.get(0);
        for (int i = 1; i < arr.size(); i++) {
            gcd = __gcd(gcd, __gcd(gcd, arr.get(i) - arr.get(i - 1)));
        }
 
        return gcd;
    }
 
    // Driver Code
    public static void main(String args[]) {
        int[] arr = { 1, 7, 3, 9, 5, 11 };
        int S = 3;
 
        System.out.println(minJumpSize(arr, S));
    }
}
 
// This code is contributed by saurabh_jaiswal.


Python3
# python program for the above approach
import math
 
# Function to find the maximum size of jump
# to reach all the coordinates
def minJumpSize(arr, S):
 
    # Pushing S into the line
    arr.append(S)
 
    # Sorting arr, to get coordinates in
    # a sorted way
    arr.sort()
 
    # Finding gcd between the differences of
    # all adjacent pairs
    gcd = arr[1] - arr[0]
    for i in range(1, len(arr)):
        gcd = math.gcd(gcd, math.gcd(gcd, arr[i] - arr[i - 1]))
 
    return gcd
 
# Driver Code
if __name__ == "__main__":
 
    arr = [1, 7, 3, 9, 5, 11]
    S = 3
 
    print(minJumpSize(arr, S))
 
# This code is contributed by rakeshsahni


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
    public static int __gcd(int a, int b) {
        if (b == 0)
            return a;
        return __gcd(b, a % b);
    }
 
    // Function to find the maximum size of jump
    // to reach all the coordinates
    public static int minJumpSize(int[] t_arr, int S) {
        List arr = new List();
 
        foreach (int x in t_arr) {
            arr.Add(x);
        }
 
        // Pushing S into the line
        arr.Add(S);
 
        // Sorting arr, to get coordinates in
        // a sorted way
        arr.Sort();
 
        // Finding gcd between the differences of
        // all adjacent pairs
        int gcd = arr[1] - arr[0];
        for (int i = 1; i < arr.Count; i++) {
            gcd = __gcd(gcd, __gcd(gcd, arr[(i)] - arr[(i - 1)]));
        }
 
        return gcd;
    }
 
 
// Driver Code
public static void Main()
{
    int[] arr = { 1, 7, 3, 9, 5, 11 };
    int S = 3;
 
    Console.WriteLine(minJumpSize(arr, S));
}
}
 
// This code is contributed by sanjoy_62.


Javascript



输出:
2

时间复杂度: O(NlogN)
辅助空间: O(1)。