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 end avoiding all obstacles. Length of every jump has to be same (For example, if we jump from 0 to 4, then we must make next jump from 4 to 8). We need to find the minimum length of jump so that we can reach 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 locations of all obstacles in a hash table. We also find maximum value of obstacle. Then we try all possible jump sizes from 1 to maximum. If any jump size leads to a obstacle, we do not consider that jump.
Java
// Java program to find length of a jump // to reach end avoiding all obstacles import java.util.*; public class obstacle { static int avoidObstacles(int[] obs) { // Insert all array elements in a hash table // and find the maximum value in the array 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]); } // checking for every possible length which // yield us solution for (int i = 1; i <= max; i++) { int j; for (j = i; j <= max; j = j + i) { // if there is obstacle, break the loop. if (hs.contains(j)) break; } // If above loop did not break if (j > max) return i; } return max+1; } // Driver Code public static void main(String[] args) { int a[] = new int[] { 5, 3, 6, 7, 9 }; int b = avoidObstacles(a); System.out.println(b); } } |
C#
// C# program to find length of a jump // to reach end avoiding all obstacles using System; using System.Collections.Generic; public class obstacle { static int avoidObstacles(int[] obs) { // Insert all array elements in a hash table // and find the maximum value in the array 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]); } // checking for every possible length which // yield us solution for (int i = 1; i <= max; i++) { int j; for (j = i; j <= max; j = j + i) { // if there is obstacle, break the loop. if (hs.Contains(j)) break; } // If above loop did not break if (j > max) return i; } return max+1; } // Driver Code public static void Main() { int []a = new int[] { 5, 3, 6, 7, 9 }; int b = avoidObstacles(a); Console.WriteLine(b); } } // This code is contributed by 29AjayKumar |
4
Recommended Posts:
- Find the minimum of maximum length of a jump required to reach the last island in exactly k jumps
- Check if it is possible to reach a number by making jumps of two given length
- Find minimum length sub-array which has given sub-sequence in it
- Minimum length subarray of 1s in a Binary Array
- Minimum number of jumps to reach end | Set 2 (O(n) solution)
- Find the Minimum length Unsorted Subarray, sorting which makes the complete array sorted
- Minimum length substring with exactly K distinct characters
- Maximum and minimum of an array using minimum number of comparisons
- Minimum sum by choosing minimum of pairs from array
- Minimum cost to reach end of array array when a maximum jump of K index is allowed
- Find minimum value to assign all array elements so that array product becomes greater
- Minimum number greater than the maximum of array which cannot be formed using the numbers in the array
- Maximum length of the sub-array whose first and last elements are same
- Length of longest sub-array with maximum arithmetic mean.
- Find maximum sum array of length less than or equal to m
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : 29AjayKumar



