 Open in App
Not now

# Maximize jump size to reach all given points from given starting point

• Last Updated : 25 Nov, 2021

Given an array, arr[] consisting of N integers representing coordinates on a number line and an integer S. Find the maximum size of jump required to visit all the coordinates at least once if the starting position is S. Also, Jumping in both directions is permitted.

Example:

Input: arr[]={1, 7, 3, 9, 5, 11}, S=3
Output: 2
Explanation: Jumps of size 2 is able to visit all the coordinates on the line, in the following way:
Jump -2: Reach 1 from 3.
Jump +2: Reach 3 from 1.
Jump +2: Reach 5 from 3.
Jump +2: Reach 7 from 5.
Jump +2: Reach 9 from 7.
Jump +2: Reach 11 from 9.

Input: arr[]={33, 105, 57}, S=81
Output: 24

Approach: An observation can be made after reading the problem, that the greatest common divisor of all the differences between two adjacent points on the line is the maximum size of Jump needed to visit all points. So, to solve this problem, follow the below steps:

1. Insert S, in the array arr, then sort it.
2. Find the gcd between the differences of all adjacent pairs in the array arr.
3. Return the final gcd as the answer to this problem.

Below is the implementation of the above approach:

## 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<``int``>& 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 - arr;``    ``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<``int``> 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<``int``> arr = ``new` `List<``int``>();` `        ``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 - arr;``        ``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

 ``

Output:

`2`

Time Complexity: O(NlogN)
Auxiliary Space: O(1).

My Personal Notes arrow_drop_up