Skip to content

Latest commit

 

History

History
32 lines (24 loc) · 752 Bytes

15.md

File metadata and controls

32 lines (24 loc) · 752 Bytes
 int minDist(int a[], int n, int x, int y) {
        // code here
        vector<int> x1;
        vector<int>y1;
        
        for(int i=0;i<n;i++)
        {
            if(a[i]==x)
                x1.push_back(i);
            
            if(a[i]==y)
                y1.push_back(i);
        }
        
        if(x1.empty() || y1.empty())
            return -1;
        
        int mini=INT_MAX;
        
        for(int i=0;i<x1.size();i++)
        {
            for(int j=0;j<y1.size();j++)
                mini=min(mini,abs(x1[i]-y1[j]));
        }
        
        return mini;
        
    }