Created
February 6, 2020 08:34
-
-
Save ruhelaanurag/a319839b19479ba3eaabbd0bf170d055 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); //HashSet contains no duplicate elements, and whose elements are in no particular order. | |
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment