Minimum length of jumps to avoid given array of obstacles
We are given coordinates of obstacles on a straight line. We start jumping from point 0, we need to reach the end, avoiding all obstacles. The length of every jump has to be the same (For example, if we jump from 0 to 4, then we must make the next jump from 4 to 8). We need to find the minimum length of the jump so that we can reach the end and we avoid all obstacles.
Examples:
Input : obs[] = [5, 3, 6, 7, 9]
Output : 4
Obstacles are at points 3, 5, 6, 7 and 9
We jump from 0 to 4, then 4 to 8, then 4
to 12. This is how we reach end with jumps
of length 4. If we try lower jump lengths,
we cannot avoid all obstacles.
Input : obs[] = [5, 8, 9, 13, 14]
Output : 6
We insert the locations of all obstacles in a hash table. We also find the maximum value of the obstacle. Then we tried all possible jump sizes from 1 to maximum. If any jump size leads to an obstacle, we do not consider that jump.
Implementation:
CPP
#include <bits/stdc++.h>
#define mod 1000000007
using namespace std;
int avoidObstacles(unordered_set< int > obs)
{
int jump_dist = 1;
bool obstacle_hit = true ;
while (obstacle_hit)
{
obstacle_hit = false ;
jump_dist += 1;
for ( auto i:obs)
{
if (i % jump_dist == 0)
{
obstacle_hit = true ;
break ;
}
}
}
return jump_dist;
}
int main()
{
unordered_set< int > a = {5, 3, 6, 7, 9};
int b = avoidObstacles(a);
cout << b;
}
|
Java
import java.util.*;
public class obstacle {
static int avoidObstacles( int [] obs)
{
HashSet<Integer> hs = new HashSet<Integer>();
int max = obs[ 0 ];
for ( int i= 0 ; i<obs.length; i++)
{
hs.add(obs[i]);
max = Math.max(max, obs[i]);
}
for ( int i = 1 ; i <= max; i++) {
int j;
for (j = i; j <= max; j = j + i) {
if (hs.contains(j))
break ;
}
if (j > max)
return i;
}
return max+ 1 ;
}
public static void main(String[] args)
{
int a[] = new int [] { 5 , 3 , 6 , 7 , 9 };
int b = avoidObstacles(a);
System.out.println(b);
}
}
|
Python3
def avoidObstacles(obs):
obs = sorted (obs)
jump_dist = 1
obstacle_hit = True
while (obstacle_hit):
obstacle_hit = False
jump_dist + = 1
for i in range ( 0 , len (obs)):
if obs[i] % jump_dist = = 0 :
obstacle_hit = True
break
return jump_dist
a = [ 5 , 3 , 6 , 7 , 9 ]
b = avoidObstacles(a)
print (b)
|
C#
using System;
using System.Collections.Generic;
public class obstacle
{
static int avoidObstacles( int [] obs)
{
HashSet< int > hs = new HashSet< int >();
int max = obs[0];
for ( int i = 0; i < obs.Length; i++)
{
hs.Add(obs[i]);
max = Math.Max(max, obs[i]);
}
for ( int i = 1; i <= max; i++)
{
int j;
for (j = i; j <= max; j = j + i)
{
if (hs.Contains(j))
break ;
}
if (j > max)
return i;
}
return max+1;
}
public static void Main()
{
int []a = new int [] { 5, 3, 6, 7, 9 };
int b = avoidObstacles(a);
Console.WriteLine(b);
}
}
|
Javascript
<script>
function avoidObstacles(obs)
{
let hs = new Set();
let max = obs[0];
for (let i=0; i<obs.length; i++)
{
hs.add(obs[i]);
max = Math.max(max, obs[i]);
}
for (let i = 1; i <= max; i++) {
let j;
for (j = i; j <= max; j = j + i) {
if (hs.has(j))
break ;
}
if (j > max)
return i;
}
return max+1;
}
let a=[5, 3, 6, 7, 9 ];
let b = avoidObstacles(a);
document.write(b);
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Last Updated :
09 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...