Sunday, March 11, 2018

#codingexercise
A step array is an array of integer where each element has a difference of atmost k with its neighbor. Given a key x, we need to find the index value of k if multiple element exist return the index of any  occurrence of key.
Input : arr[] = {4, 5, 6, 7, 6}
           k = 1
           x = 6
Output : 2


int GetIndex(List<int> A, int x, int k)
{
int i = 0;
while (i < A.Length)
{
if (A[i] == x)
   return i;
i = i + Math.Max(1, Math.Abs(A[i]-x)/k);
}
return -1;
}

Get the nth number of the Golomb sequence

int a (uint n)
{
if ( n == 0)
      return 0;
if  (n == 1) 
      return 1;
return 1 + a(n – a(a(n-1)));
}


No comments:

Post a Comment