Dada una array desordenada arr[] y dos números x e y , encuentre la distancia mínima entre x e y en arr[] . La array también puede contener duplicados. Puede suponer que tanto x como y son diferentes y están presentes en arr[] .
Ejemplos:
C++
// C++ program to Find the minimum // distance between two numbers #include <bits/stdc++.h> using namespace std; int minDist(int arr[], int n, int x, int y) { int i, j; int min_dist = INT_MAX; for (i = 0; i < n; i++) { for (j = i + 1; j < n; j++) { if ((x == arr[i] && y == arr[j] || y == arr[i] && x == arr[j]) && min_dist > abs(i - j)) { min_dist = abs(i - j); } } } if (min_dist > n) { return -1; } return min_dist; } /* Driver code */ int main() { int arr[] = { 3, 5, 4, 2, 6, 5, 6, 6, 5, 4, 8, 3 }; int n = sizeof(arr) / sizeof(arr[0]); int x = 3; int y = 6; cout << "Minimum distance between " << x << " and " << y << " is " << minDist(arr, n, x, y) << endl; } // This code is contributed by Shivi_Aggarwal
C
// C program to Find the minimum // distance between two numbers #include <limits.h> // for INT_MAX #include <stdio.h> #include <stdlib.h> // for abs() int minDist(int arr[], int n, int x, int y) { int i, j; int min_dist = INT_MAX; for (i = 0; i < n; i++) { for (j = i + 1; j < n; j++) { if ((x == arr[i] && y == arr[j] || y == arr[i] && x == arr[j]) && min_dist > abs(i - j)) { min_dist = abs(i - j); } } } if (min_dist > n) { return -1; } return min_dist; } /* Driver program to test above function */ int main() { int arr[] = { 3, 5, 4, 2, 6, 5, 6, 6, 5, 4, 8, 3 }; int n = sizeof(arr) / sizeof(arr[0]); int x = 0; int y = 6; printf("Minimum distance between %d and %d is %d\n", x, y, minDist(arr, n, x, y)); return 0; }
Java
// Java Program to Find the minimum // distance between two numbers class MinimumDistance { int minDist(int arr[], int n, int x, int y) { int i, j; int min_dist = Integer.MAX_VALUE; for (i = 0; i < n; i++) { for (j = i + 1; j < n; j++) { if ((x == arr[i] && y == arr[j] || y == arr[i] && x == arr[j]) && min_dist > Math.abs(i - j)) min_dist = Math.abs(i - j); } } if (min_dist > n) { return -1; } return min_dist; } public static void main(String[] args) { MinimumDistance min = new MinimumDistance(); int arr[] = { 3, 5, 4, 2, 6, 5, 6, 6, 5, 4, 8, 3 }; int n = arr.length; int x = 0; int y = 6; System.out.println("Minimum distance between " + x + " and " + y + " is " + min.minDist(arr, n, x, y)); } }
Python3
# Python3 code to Find the minimum # distance between two numbers def minDist(arr, n, x, y): min_dist = 99999999 for i in range(n): for j in range(i + 1, n): if (x == arr[i] and y == arr[j] or y == arr[i] and x == arr[j]) and min_dist > abs(i-j): min_dist = abs(i-j) return min_dist # Driver code arr = [3, 5, 4, 2, 6, 5, 6, 6, 5, 4, 8, 3] n = len(arr) x = 3 y = 6 print("Minimum distance between ", x, " and ", y, "is", minDist(arr, n, x, y)) # This code is contributed by "Abhishek Sharma 44"
C#
// C# code to Find the minimum // distance between two numbers using System; class GFG { static int minDist(int []arr, int n, int x, int y) { int i, j; int min_dist = int.MaxValue; for (i = 0; i < n; i++) { for (j = i + 1; j < n; j++) { if ((x == arr[i] && y == arr[j] || y == arr[i] && x == arr[j]) && min_dist > Math.Abs(i - j)) min_dist = Math.Abs(i - j); } } return min_dist; } // Driver function public static void Main() { int []arr = {3, 5, 4, 2, 6, 5, 6, 6, 5, 4, 8, 3}; int n = arr.Length; int x = 3; int y = 6; Console.WriteLine("Minimum " + "distance between " + x + " and " + y + " is " + minDist(arr, n, x, y)); } } // This code is contributed by Sam007
PHP
<?php // PHP program to Find the minimum // distance between two numbers function minDist($arr, $n, $x, $y) { $i; $j; $min_dist = PHP_INT_MAX; for ($i = 0; $i < $n; $i++) { for ($j = $i + 1; $j < $n; $j++) { if( ($x == $arr[$i] and $y == $arr[$j] or $y == $arr[$i] and $x == $arr[$j]) and $min_dist > abs($i - $j)) { $min_dist = abs($i - $j); } } } if($min_dist>$n) { return -1; } return $min_dist; } // Driver Code $arr = array(3, 5, 4, 2, 6, 5, 6, 6, 5, 4, 8, 3); $n = count($arr); $x = 0; $y = 6; echo "Minimum distance between ",$x, " and ",$y," is "; echo minDist($arr, $n, $x, $y); // This code is contributed by anuj_67. ?>
Javascript
<script> // Javascript program to find the minimum // distance between two numbers function minDist(arr, n, x, y) { var i, j; var min_dist = Number.MAX_VALUE; for(i = 0; i < n; i++) { for(j = i + 1; j < n; j++) { if ((x == arr[i] && y == arr[j] || y == arr[i] && x == arr[j]) && min_dist > Math.abs(i - j)) min_dist = Math.abs(i - j); } } if(min_dist>n) { return -1; } return min_dist; } // Driver code var arr = [ 3, 5, 4, 2, 6, 5, 6, 6, 5, 4, 8, 3 ]; var n = arr.length; var x = 3; var y = 6; document.write("Minimum distance between " + x + " and " + y + " is " + minDist(arr, n, x, y)); // This code is contributed by gauravrajput1 </script>
C++
// C++ implementation of above approach #include <bits/stdc++.h> using namespace std; int minDist(int arr[], int n, int x, int y) { //previous index and min distance int p = -1, min_dist = INT_MAX; for(int i=0 ; i<n ; i++) { if(arr[i]==x || arr[i]==y) { //we will check if p is not equal to -1 and //If the element at current index matches with //the element at index p , If yes then update //the minimum distance if needed if( p != -1 && arr[i] != arr[p]) min_dist = min(min_dist , i-p); //update the previous index p=i; } } //If distance is equal to int max if(min_dist==INT_MAX) return -1; return min_dist; } /* Driver code */ int main() { int arr[] = {3, 5, 4, 2, 6, 3, 0, 0, 5, 4, 8, 3}; int n = sizeof(arr) / sizeof(arr[0]); int x = 3; int y = 6; cout << "Minimum distance between " << x << " and " << y << " is "<< minDist(arr, n, x, y) << endl; return 0; } // This code is contributed by Mukul singh.
C
#include <stdio.h> #include <limits.h> // For INT_MAX //returns minimum of two numbers int min(int a ,int b) { if(a < b) return a; return b; } int minDist(int arr[], int n, int x, int y) { //previous index and min distance int i=0,p=-1, min_dist=INT_MAX; for(i=0 ; i<n ; i++) { if(arr[i] ==x || arr[i] == y) { //we will check if p is not equal to -1 and //If the element at current index matches with //the element at index p , If yes then update //the minimum distance if needed if(p != -1 && arr[i] != arr[p]) min_dist = min(min_dist,i-p); //update the previous index p=i; } } //If distance is equal to int max if(min_dist==INT_MAX) return -1; return min_dist; } /* Driver program to test above function */ int main() { int arr[] ={3, 5, 4, 2, 6, 3, 0, 0, 5, 4, 8, 3}; int n = sizeof(arr)/sizeof(arr[0]); int x = 3; int y = 6; printf("Minimum distance between %d and %d is %d\n", x, y, minDist(arr, n, x, y)); return 0; }
Java
class MinimumDistance { int minDist(int arr[], int n, int x, int y) { //previous index and min distance int i=0,p=-1, min_dist=Integer.MAX_VALUE; for(i=0 ; i<n ; i++) { if(arr[i] ==x || arr[i] == y) { //we will check if p is not equal to -1 and //If the element at current index matches with //the element at index p , If yes then update //the minimum distance if needed if(p != -1 && arr[i] != arr[p]) min_dist = Math.min(min_dist,i-p); //update the previous index p=i; } } //If distance is equal to int max if(min_dist==Integer.MAX_VALUE) return -1; return min_dist; } /* Driver program to test above functions */ public static void main(String[] args) { MinimumDistance min = new MinimumDistance(); int arr[] = {3, 5, 4, 2, 6, 3, 0, 0, 5, 4, 8, 3}; int n = arr.length; int x = 3; int y = 6; System.out.println("Minimum distance between " + x + " and " + y + " is " + min.minDist(arr, n, x, y)); } }
Python3
import sys def minDist(arr, n, x, y): #previous index and min distance i=0 p=-1 min_dist = sys.maxsize; for i in range(n): if(arr[i] ==x or arr[i] == y): #we will check if p is not equal to -1 and #If the element at current index matches with #the element at index p , If yes then update #the minimum distance if needed if(p != -1 and arr[i] != arr[p]): min_dist = min(min_dist,i-p) #update the previous index p=i #If distance is equal to int max if(min_dist == sys.maxsize): return -1 return min_dist # Driver program to test above function */ arr = [3, 5, 4, 2, 6, 3, 0, 0, 5, 4, 8, 3] n = len(arr) x = 3 y = 6 print ("Minimum distance between %d and %d is %d\n"%( x, y,minDist(arr, n, x, y))); # This code is contributed by Shreyanshi Arun.
C#
// C# program to Find the minimum // distance between two numbers using System; class MinimumDistance { static int minDist(int []arr, int n, int x, int y) { //previous index and min distance int i=0,p=-1, min_dist=int.MaxValue; for(i=0 ; i<n ; i++) { if(arr[i] ==x || arr[i] == y) { //we will check if p is not equal to -1 and //If the element at current index matches with //the element at index p , If yes then update //the minimum distance if needed if(p != -1 && arr[i] != arr[p]) min_dist = Math.Min(min_dist,i-p); //update the previous index p=i; } } //If distance is equal to int max if(min_dist==int.MaxValue) return -1; return min_dist; } // Driver Code public static void Main() { int []arr = {3, 5, 4, 2, 6, 3, 0, 0, 5, 4, 8, 3}; int n = arr.Length; int x = 3; int y = 6; Console.WriteLine("Minimum distance between " + x + " and " + y + " is " + minDist(arr, n, x, y)); } } // This code is contributed by anuj_67.
PHP
<?php // PHP program to Find the minimum // distance between two numbers function minDist($arr, $n, $x, $y) { //previous index and min distance $i=0; $p=-1; $min_dist=PHP_INT_MAX; for($i=0 ; $i<$n ; $i++) { if($arr[$i] ==$x || $arr[$i] == $y) { //we will check if p is not equal to -1 and //If the element at current index matches with //the element at index p , If yes then update //the minimum distance if needed if($p != -1 && $arr[$i] != $arr[$p]) $min_dist = min($min_dist,$i-$p); //update the previous index $p=$i; } } //If distance is equal to int max if($min_dist==PHP_INT_MAX) return -1; return $min_dist; } /* Driver program to test above function */ $arr =array(3, 5, 4, 2, 6, 3, 0, 0, 5, 4, 8, 3); $n = count($arr); $x = 3; $y = 6; echo "Minimum distance between $x and ", "$y is ", minDist($arr, $n, $x, $y); // This code is contributed by anuj_67. ?>
Javascript
<script> function minDist(arr , n , x , y) { // previous index and min distance var i=0,p=-1, min_dist=Number.MAX_VALUE; for(i=0 ; i<n ; i++) { if(arr[i] ==x || arr[i] == y) { // we will check if p is not equal to -1 and // If the element at current index matches with // the element at index p , If yes then update // the minimum distance if needed if(p != -1 && arr[i] != arr[p]) min_dist = Math.min(min_dist,i-p); // update the previous index p=i; } } // If distance is equal to var max if(min_dist==Number.MAX_VALUE) return -1; return min_dist; } /* Driver program to test above functions */ var arr = [3, 5, 4, 2, 6, 3, 0, 0, 5, 4, 8, 3]; var n = arr.length; var x = 3; var y = 6; document.write("Minimum distance between " + x + " and " + y + " is " + minDist(arr, n, x, y)); // This code contributed by shikhasingrajput </script>
C++
// C++ program to Find the minimum // distance between two numbers #include <bits/stdc++.h> using namespace std; int minDist(int arr[], int n, int x, int y) { //idx1 and idx2 will store indices of //x or y and min_dist will store the minimum difference int idx1=-1,idx2=-1,min_dist = INT_MAX; for(int i=0;i<n;i++) { //if current element is x then change idx1 if(arr[i]==x) { idx1=i; } //if current element is y then change idx2 else if(arr[i]==y) { idx2=i; } //if x and y both found in array //then only find the difference and store it in min_dist if(idx1!=-1 && idx2!=-1) min_dist=min(min_dist,abs(idx1-idx2)); } //if left or right did not found in array //then return -1 if(idx1==-1||idx2==-1) return -1; //return the minimum distance else return min_dist; } /* Driver code */ int main() { int arr[] = { 3, 5, 4, 2, 6, 5, 6, 6, 5, 4, 8, 3 }; int n = sizeof(arr) / sizeof(arr[0]); int x = 3; int y = 6; cout << "Minimum distance between " << x << " and " << y << " is " << minDist(arr, n, x, y) << endl; }
Java
// Java program to Find the minimum // distance between two numbers public class GFG { static int minDist(int arr[], int n, int x, int y) { // idx1 and idx2 will store indices of // x or y and min_dist will store the minimum // difference int idx1 = -1, idx2 = -1, min_dist = Integer.MAX_VALUE; for (int i = 0; i < n; i++) { // if current element is x then change idx1 if (arr[i] == x) { idx1 = i; } // if current element is y then change idx2 else if (arr[i] == y) { idx2 = i; } // if x and y both found in array // then only find the difference and store it in // min_dist if (idx1 != -1 && idx2 != -1) min_dist = Math.min(min_dist, Math.abs(idx1 - idx2)); } // if left or right did not found in array // then return -1 if (idx1 == -1 || idx2 == -1) return -1; // return the minimum distance else return min_dist; } /* Driver code */ public static void main(String[] args) { int arr[] = { 3, 5, 4, 2, 6, 5, 6, 6, 5, 4, 8, 3 }; int n = arr.length; int x = 3; int y = 6; System.out.println("Minimum distance between " + x + " and " + y + " is " + minDist(arr, n, x, y)); } } // This code is contributed by Lovely Jain
Python3
# Python program to Find the minimum # distance between two numbers import sys def minDist(arr, n, x, y) : # idx1 and idx2 will store indices of # x or y and min_dist will store the minimum difference idx1=-1; idx2=-1; min_dist = sys.maxsize; for i in range(n) : # if current element is x then change idx1 if arr[i]==x : idx1=i # if current element is y then change idx2 elif arr[i]==y : idx2=i # if x and y both found in array # then only find the difference and store it in min_dist if idx1!=-1 and idx2!=-1 : min_dist=min(min_dist,abs(idx1-idx2)); # if left or right did not found in array # then return -1 if idx1==-1 or idx2==-1 : return -1 # return the minimum distance else : return min_dist # Driver code if __name__ == "__main__" : arr = [ 3, 5, 4, 2, 6, 5, 6, 6, 5, 4, 8, 3] n = len(arr) x = 3 y = 6 print ("Minimum distance between %d and %d is %d\n"%( x, y,minDist(arr, n, x, y))); # this code is contributed by aditya942003patil
C#
// C# program to Find the minimum // distance between two numbers using System; class MinimumDistance { static int minDist(int []arr, int n, int x, int y) { //idx1 and idx2 will store indices of //x or y and min_dist will store the minimum difference int idx1=-1,idx2=-1,min_dist = int.MaxValue; for(int i=0;i<n;i++) { //if current element is x then change idx1 if(arr[i]==x) { idx1=i; } //if current element is y then change idx2 else if(arr[i]==y) { idx2=i; } //if x and y both found in array //then only find the difference and store it in min_dist if(idx1!=-1 && idx2!=-1) min_dist=Math.Min(min_dist,Math.Abs(idx1-idx2)); } //if left or right did not found in array //then return -1 if(idx1==-1||idx2==-1) return -1; //return the minimum distance else return min_dist; } // Driver Code public static void Main() { int []arr = { 3, 5, 4, 2, 6, 5, 6, 6, 5, 4, 8, 3}; int n = arr.Length; int x = 3; int y = 6; Console.WriteLine("Minimum distance between " + x + " and " + y + " is " + minDist(arr, n, x, y)); } } // This code is contributed by aditya942003patil.
Javascript
<script> function minDist(arr , n , x , y) { //idx1 and idx2 will store indices of //x or y and min_dist will store the minimum difference var idx1=-1,idx2=-1,min_dist = Number.MAX_VALUE; for(var i=0;i<n;i++) { //if current element is x then change idx1 if(arr[i]==x) { idx1=i; } //if current element is y then change idx2 else if(arr[i]==y) { idx2=i; } //if x and y both found in array //then only find the difference and store it in min_dist if(idx1!=-1 && idx2!=-1) min_dist=Math.min(min_dist,Math.abs(idx1-idx2)); } //if left or right did not found in array //then return -1 if(idx1==-1||idx2==-1) return -1; //return the minimum distance else return min_dist; } /* Driver program to test above functions */ var arr = [ 3, 5, 4, 2, 6, 5, 6, 6, 5, 4, 8, 3]; var n = arr.length; var x = 3; var y = 6; document.write("Minimum distance between " + x + " and " + y + " is " + minDist(arr, n, x, y)); // This code contributed by aditya942003patil </script>
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA