Skip to content

Instantly share code, notes, and snippets.

@ruhelaanurag
Created February 6, 2020 08:34
Show Gist options
  • Save ruhelaanurag/a319839b19479ba3eaabbd0bf170d055 to your computer and use it in GitHub Desktop.
Save ruhelaanurag/a319839b19479ba3eaabbd0bf170d055 to your computer and use it in GitHub Desktop.
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