read everything once.... insert them in a dictionary of type <char,
list<int>> where for each character key, u record the indices (since u
record from left to right, they're gonna be in ascending order)

then, for any character that's asked for, u can get to the list in
O(1) time and can get the min distance...

On Jun 30, 12:40 pm, bittu <shashank7andr...@gmail.com> wrote:
> lets take the example {1,2, 10, 2, 3, 5, 1, 1,2,3,5,2} clearly min
> distance is 1 between a=2 & b=5
> take variables current=-1 & min=INT_Max
> so we start from last element say its index is last which is array
> size -1 & check it with a & b if its equal to any of these then check
> current !=- && a[current]!=a[last] if its true then compare min with
> min & current-last & update the min e.g min=minimum(min,current-last)
> set current=n; repeat until we run out of loop
>
> so function looks like
> minDistance (int a[], int n, int b, int c)
> {
>
> int ret = INT_MAX, cur rent= -1;
>
> while(n--)
> {
>
> if(a[n] == b || a[n] == c) {
>
> if(cur rent!= -1 && a[current] != a[n]) {
>
> min = minimum(min, current - n);}
>
> current = n//upodate current
>
> }
>
> Thanks
> Shashank Mani
> CSE, BIT Mesra

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to