@ashsih ..here is algo 1st Traverse array from left side and stop if either *a* or *b *are found. Store index of this first occurrence in a variable say first then Now traverse *array *after the index *first*. If the element at current index *i* matches with either x or y then check if it is different from * arr[first]* that we have already found. If it is different then update the minimum distance so far we have traversed the array . If it is same then update *first * i.e., make *first = i*. repeat same again .
time complexity O(N) , space complexity O(1) Hope you can implement it :) Let me if other way exist to do the same or any flaw in approach ? *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/-/MUpEZqJS1NcJ. 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.
