# 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
```

*

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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.

## CPP

 `// C++ program to find length of a jump ` `// to reach end avoiding all obstacles  ` `#include ` `#define mod 1000000007 ` `using` `namespace` `std; ` ` `  `int` `avoidObstacles(unordered_set<``int``> obs) ` `{ ` ` `  `    ``// set jump distance to 1 ` `    ``int` `jump_dist = 1; ` ` `  `    ``// flag to check if current jump distance ` `    ``// hits an obstacle ` `    ``bool` `obstacle_hit = ``true``; ` ` `  `    ``while``(obstacle_hit) ` `    ``{ ` ` `  `        ``obstacle_hit = ``false``; ` `        ``jump_dist += 1; ` ` `  `        ``// checking if jumping with current length ` `        ``// hits an obstacle ` `        ``for` `(``auto` `i:obs) ` `        ``{ ` `            ``if` `(i % jump_dist == 0) ` `            ``{ ` ` `  `                ``// if obstacle is hit repeat process ` `                ``// after increasing jump distance ` `                ``obstacle_hit = ``true``; ` `                ``break``; ` `            ``} ` `            ``} ` `        ``}  ` ` `  `    ``return` `jump_dist; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``unordered_set<``int``> a = {5, 3, 6, 7, 9}; ` `    ``int` `b = avoidObstacles(a); ` `    ``cout << b; ` `} ` ` `  `// This code is contributed by mohit kumar 29 `

## 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 hs = ``new` `HashSet(); ` `        ``int` `max = obs[``0``]; ` `        ``for` `(``int` `i=``0``; i 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); ` `    ``} ` `} `

## Python3

 `# Python3 program to find length of a jump ` `# to reach end avoiding all obstacles ` `def` `avoidObstacles(obs): ` `     `  `    ``# sort the list in ascending order ` `    ``obs ``=` `sorted``(obs) ` `     `  `    ``# set jump distance to 1 ` `    ``jump_dist ``=` `1` `     `  `    ``# flag to check if current jump distance  ` `    ``# hits an obstacle ` `    ``obstacle_hit ``=` `True` ` `  `    ``while``(obstacle_hit): ` `         `  `        ``obstacle_hit ``=` `False` `        ``jump_dist ``+``=` `1` `         `  `        ``# checking if jumping with current length ` `        ``# hits an obstacle ` `        ``for` `i ``in` `range``(``0``, ``len``(obs)): ` `            ``if` `obs[i] ``%` `jump_dist ``=``=` `0``: ` `                 `  `                ``# if obstacle is hit repeat process ` `                ``# after increasing jump distance ` `                ``obstacle_hit ``=` `True` `                ``break` ` `  `    ``return` `jump_dist ` `     `  `# Driver Code ` `a ``=` `[``5``, ``3``, ``6``, ``7``, ``9``] ` `b ``=` `avoidObstacles(a) ` `print``(b) ` ` `  `# This code is contributed by ViratJoshi `

## 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; ` `        ``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 `

Output:

```4
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
{"ft_lang_label":"__label__en","ft_lang_prob":0.7451248,"math_prob":0.8485492,"size":6125,"snap":"2020-34-2020-40","text_gpt3_token_len":1735,"char_repetition_ratio":0.13445516,"word_repetition_ratio":0.2138047,"special_character_ratio":0.2945306,"punctuation_ratio":0.11878686,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99508506,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-09-21T16:19:45Z\",\"WARC-Record-ID\":\"<urn:uuid:282c4a84-f101-4d59-ac7e-156790226828>\",\"Content-Length\":\"135383\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:2d8f59af-0569-47b3-9393-31a656fd8da5>\",\"WARC-Concurrent-To\":\"<urn:uuid:33e4de18-a161-4023-9912-1acaababbbb4>\",\"WARC-IP-Address\":\"23.199.71.163\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/minimum-length-of-jumps-to-avoid-given-array-of-obstacles/\",\"WARC-Payload-Digest\":\"sha1:OD3KONEPGCEEOOHL7GTHSMXLTFOTIXVI\",\"WARC-Block-Digest\":\"sha1:NQOOWURPSI5AIZXSGORJIAPLCFBXZPU6\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-40/CC-MAIN-2020-40_segments_1600400201826.20_warc_CC-MAIN-20200921143722-20200921173722-00244.warc.gz\"}"}