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:
- Insert S, in the array arr, then sort it.
- Find the gcd between the differences of all adjacent pairs in the array arr.
- Return the final gcd as the answer to this problem.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minJumpSize(vector< int >& arr, int S)
{
arr.push_back(S);
sort(arr.begin(), arr.end());
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;
}
int main()
{
vector< int > arr = { 1, 7, 3, 9, 5, 11 };
int S = 3;
cout << minJumpSize(arr, S);
}
|
Java
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);
}
public static int minJumpSize( int [] t_arr, int S) {
ArrayList<Integer> arr = new ArrayList<Integer>();
for ( int x : t_arr) {
arr.add(x);
}
arr.add(S);
Collections.sort(arr);
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;
}
public static void main(String args[]) {
int [] arr = { 1 , 7 , 3 , 9 , 5 , 11 };
int S = 3 ;
System.out.println(minJumpSize(arr, S));
}
}
|
Python3
import math
def minJumpSize(arr, S):
arr.append(S)
arr.sort()
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
if __name__ = = "__main__" :
arr = [ 1 , 7 , 3 , 9 , 5 , 11 ]
S = 3
print (minJumpSize(arr, S))
|
C#
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);
}
public static int minJumpSize( int [] t_arr, int S) {
List< int > arr = new List< int >();
foreach ( int x in t_arr) {
arr.Add(x);
}
arr.Add(S);
arr.Sort();
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;
}
public static void Main()
{
int [] arr = { 1, 7, 3, 9, 5, 11 };
int S = 3;
Console.WriteLine(minJumpSize(arr, S));
}
}
|
Javascript
<script>
function __gcd(a, b) {
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return __gcd(a - b, b);
return __gcd(a, b - a);
}
function minJumpSize(arr, S) {
arr.push(S);
arr.sort( function (a, b) { return a - b })
let gcd = arr[1] - arr[0];
for (let i = 1; i < arr.length; i++) {
gcd = __gcd(gcd,
__gcd(gcd,
arr[i] - arr[i - 1]));
}
return gcd;
}
let arr = [1, 7, 3, 9, 5, 11];
let S = 3;
document.write(minJumpSize(arr, S))
</script>
|
Time Complexity: O(NlogN)
Auxiliary Space: O(1).
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...