Minimum distance between two numbers | Practice | GeeksforGeeks

You are given an array A, of N elements. Find minimum index based distance between two elements of the array, x and y.
Example 1:
Input:
N = 4
A[] = {1,2,3,2}
x = 1, y = 2
Output: 1
Explanation: x = 1 and y = 2. There are
two distances between x and y, which are
1 and 3 out of which the least is 1.

Example 2:
Input:
N = 7
A[] = {86,39,90,67,84,66,62}
x = 42, y = 12
Output: -1
Explanation: x = 42 and y = 12. We return
-1 as x and y don't exist in the array.
Your Task:
Complete the function minDist() which takes the array, n, x and y as input parameters and returns the minimum distance between x and y in the array. If no such distance is possible then return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 105
0 <= A[i], x, y <= 105
+2
harshscode4 days ago
 int p=-1,q=-1,mi=INT_MAX;       for(int i=0;i<n;i++)       {           if(a[i]==x)           p=i;           if(a[i]==y)           q=i;                      if(p!=-1 and q!=-1)           mi=min(mi,abs(p-q));       }              if(p==-1 or q==-1)       return -1;       else       return mi;
+1
shilsoumyadip6 days ago
 int mini = INT_MAX;       int a1 = -1;       int a2 = -1;       for ( int i = 0 ; i< n ; i++)       {           if (a[i] == x ) a1 = i;           if (a[i] == y ) a2 = i;                      if(a1 != -1 and a2 != -1)           {               mini = min(mini , abs(a1-a2));           }       }                  if(a1 == -1 or a2 == -1 ) return -1;       return mini;   }
0
zerefkhan2 weeks ago
Solution of C++
Time taken : 0.67/1.83
 
	int minDist(int a[], int n, int x, int y) {
        int min_dist = INT_MAX; // Setting min_dist to the maximum vaalue possible
        int x_pos = -1, y_pos = -1; // Assigning initial '-1' value to x_pos and y_pos
        for(int i = 0; i < n ; i++)
        {
            if(a[i] == x){
                x_pos = i;
                if(x == y) return 0;
            } // Position of x
            if(a[i] == y) y_pos = i; // Position of y
            if(x_pos > -1 && y_pos > -1) min_dist = min(min_dist, abs(x_pos - y_pos));
        }
        return (x_pos == -1 || y_pos == -1)?-1:min_dist;
    }
0
yankur20022 weeks ago
C++ Solution:
class Solution{ public:   int minDist(int a[], int n, int x, int y) {   int l=-1,r=-1;       int m=n;       int diff=n;       for(int i=0;i<n;i++)       {           if (a[i]==x)           l=i;           if (a[i]==y)           r=i;           if (l>=0 && r>=0)           diff = abs(r-l);                      m=min(diff,m);       }       if (m==n)       return -1;       else       return m;   }};
+1
godvishalkumar7863 weeks ago
 //java        int f=-1;       int s=-1;       int ans=Integer.MAX_VALUE;       for(int i=0;i<a.length; i++){           if(a[i]==x){                f=i;           }           else if(a[i]==y){               s=i;           }           if(s!=-1 && f!=-1){               int temp= Math.abs(s-f);               ans= Math.min(temp,ans);           }       }
       if(s==-1 || f==-1){           return -1;       }
       return ans;   }}
+1
mayank20214 weeks ago
C++ : 0.7/1.8int minDist(int a[], int n, int x, int y) {       int minD=INT_MAX, minX=-1, minY=-1;              for(int i=0; i<n ; i++)       {           if(a[i]==x)            minX=i;           else if(a[i]==y)            minY=i;                        if(minX>-1 && minY>-1)             minD=min(minD, abs(minX-minY));                   }                     return (minD==INT_MAX)?-1:minD;          }
0
swapniltayal4224 weeks ago
    def minDist(self, arr, n, x, y):              cx = -1       cy = -1       ans = 1000000       for i in range(n):           if arr[i] == x:               cx = i           if arr[i] == y:               cy = i                          if (cx != -1 and cy != -1):               ans = min(ans, abs(cx - cy))                                     if (cx == -1 or cy == -1):           return -1
       return ans
0
aditya_mishra4 weeks ago
CPP Solution
 
class Solution{ public:   int minDist(int a[], int n, int x, int y) {       // code here       int xIndex= -1, yIndex= -1;       int res = INT_MAX;       for(int i =0; i<n; i++){           if(a[i] == x){               xIndex = i;           }           else if(a[i]==y){               yIndex = i;           }           if(xIndex != -1 && yIndex != -1){               int temp = abs(xIndex-yIndex);               res = min(res, temp);           }       }              if(xIndex == -1 || yIndex == -1)           return -1;              return res;          }};
+1
madhukartemba4 weeks ago
JAVA SOLUTION:
class Solution {
    int minDist(int a[], int n, int x, int y) {
        // code here
        int min_dist = Integer.MAX_VALUE;
        
        int st = -1, en = -1;
        
        for(int i=0; i<n; i++)
        {
            if(a[i]==x)
            {
                st = i;
            }
            else if(a[i]==y)
            {
                en = i;
            }
            
            if(st!=-1 && en!=-1)
            {
                min_dist = Math.min(min_dist, Math.abs(en-st));
            }
            
        }
        
        return min_dist==Integer.MAX_VALUE ? -1 : min_dist;
        
        
    }
}
+1
prishita4 weeks ago
JAVA Solution
class Solution {
    int minDist(int a[], int n, int x, int y) {
        // code here
        int min=Integer.MAX_VALUE;
        int d1=-1,d2=-1;
        for(int i=0;i<n;i++){
            if(a[i]==x) d1=i;
            if(a[i]==y) d2=i;
            if(d1!=-1 && d2!=-1){
                min=Math.min(min,Math.abs(d1-d2));
            }
        }
        if(d1 == -1 || d2 == -1) return -1;
        return min; 
    }
}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.