chaitanya.jun12
BAN USERint return_first_1(int * a, int n)
{
int low = 0;
int high = n-1;
int mid;
while(1)
{
mid = (low+high)/2;
if((mid >= 0) && (mid < n) && (low <= high))
{
if( a[mid] == 0 ) {
if(((mid+1) < n) && a[mid+1] == 1) {
return (mid+1);
} else {
if(mid == 0)
return 0;
low = mid+1;
}
} else if( a[mid] == 1 ) {
if((mid-1) >= 0 && a[mid-1] == 0) {
return mid;
} else {
if(mid == 0)
return 0;
high = mid-1;
}
}
} else {
break;
}
}
return -1;
}
int first_occurence(int * a, int n, int key)
{
if (!n)
return -1;
int diff = abs(key - a[0]);
while(diff < n) {
if(a[diff] == key) {
return diff;
} else {
int t = abs(a[diff] - key);
diff = diff + t;
}
}
return -1;
}
int match(int * a, int m, int * b, int n)
{
if(m == 0 || n == 0)
return -1;
int flag = 0, highest = -1;
for (int i=0; i < m; i++)
{
int k = 0;
int j = (n-1);
while(1)
{
if(k <= i && j>=(n-1-i))
{
if(a[k] != b[j])
{
flag = 1;
break;
}
} else {
break;
}
k = k + 1;
j = j - 1;
}
if(flag != 1)
highest = i;
}
return highest;
}
void reverse(char * start, char * end)
{
if(start == NULL || end == NULL)
return;
else if(start == end)
return;
char * begin = NULL;
char * stop = NULL;
if(start > end) {
begin = end;
stop = start;
} else {
begin = start;
stop = end;
}
while (1) {
if(begin != NULL && stop != NULL) {
char t = *begin;
*begin = *stop;
*stop = t;
}
begin++;
stop--;
if(begin == stop || (begin+1 == stop && begin == stop-1))
return;
}
}
void recurreverse(char *t)
{
if(*t == '\0')
return;
recurreverse(t+1);
cout << *t;
}
By the way, how will we go to the middle of the linked list, we are not given the size of the linked list. Of course we can use an extra pointer to start traverse till the end, find its length and then goto middle. But, we are not supposed to use any extra memory.
- chaitanya.jun12 May 21, 2013