Monday, April 11, 2011

WAP to Fine Min Distance between two Number in Different List , Each Word May Occur at Differnt Position

You have two lists, each containing position of a word in some document. Write a program that returns minimum distance between the words in the document.
For example: Suppose X occurs at places {2, 3, 5, 10, 12, 16, 19, 20} and Y occurs at {8, 14, 27, 29}, then the minimum distance between X and Y is 1 (X=12,Y=14 OR X=16,Y=14).


#include
#include
#include

int min_Dist(int a[], int m, int b[], int n)
{

int i = 0,j= 0,min = INT_MAX,cur;

while(i < m && j < n)
{
cur = abs(a[i] - b[j]);

if(cur min=cur;
else
min=min;

if(a[i] < b[j])
i++;
else
j++;
}

return min;
}

int main() {

int a[] ={2, 3, 5, 10, 12, 16, 19, 20};//{2, 3, 5, 11, 18, 19, 20};
int b[]= {8, 14, 27, 29};//{15, 24, 27, 29};
int a_size=sizeof(a)/sizeof(int);
int b_size=sizeof(b)/sizeof(int);

printf("%d\n", min_Dist(a,a_size,b,b_size));

return 0;
}


Run Here https://ideone.com/5qIcB

No comments :