@harsahl that won't work in duplicate case :)
@ashish ..my will produce correct result , no need of using extra variable
here is main function i am talking about
int minDist(char arr[], int n, char x, char y)
{
int i = 0;
int min_dist = INT_MAX;
int first;
// Find the first occurence of any of the two numbers (x or y)
// and store the index of this occurence in prev
for (i = 0; i < n; i++)
{
if (arr[i] == x || arr[i] == y)
{
first= i;
break;
}
}
// Traverse after the first occurence
for ( ; i < n; i++)
{
if (arr[i] == x || arr[i] == y)
{
// If the current element matches with any of the two then
// check if current element and prev element are different
// Also check if this value is smaller than minimm distance so far
if ( arr[first] != arr[i] && (i - first) < min_dist )
{
min_dist = i - first;
first= i;
}
else
first = i;
}
}
return min_dist;
}
correct me if anything wrong ?
*Thanks
Shashank Mani Narayan
Computer Science & Engineering
Birla Institute of Technology,Mesra
** Founder Cracking The Code Lab "http://shashank7s.blogspot.com/"*
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/algogeeks/-/8jtKYhSvwYQJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.