<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 找到兩個數字之間的最小距離 > 原文: [https://www.geeksforgeeks.org/find-the-minimum-distance-between-two-numbers/](https://www.geeksforgeeks.org/find-the-minimum-distance-between-two-numbers/) 給定未排序的數組`arr[]`和兩個數字`x`和`y`,找出`x`和`y`在`arr[]`中的最小距離。 數組也可能包含重復項。 您可以假設`x`和`y`都不相同,并且存在于`arr[]`中。 **示例**: ``` Input: arr[] = {1, 2}, x = 1, y = 2 Output: Minimum distance between 1 and 2 is 1. Explanation: 1 is at index 0 and 2 is at index 1, so the distance is 1 Input: arr[] = {3, 4, 5}, x = 3, y = 5 Output: Minimum distance between 3 and 5 is 2. Explanation:3 is at index 0 and 5 is at index 2, so the distance is 2 Input: arr[] = {3, 5, 4, 2, 6, 5, 6, 6, 5, 4, 8, 3}, x = 3, y = 6 Output: Minimum distance between 3 and 6 is 4. Explanation:3 is at index 0 and 6 is at index 5, so the distance is 4 Input: arr[] = {2, 5, 3, 5, 4, 4, 2, 3}, x = 3, y = 2 Output: Minimum distance between 3 and 2 is 1. Explanation:3 is at index 7 and 2 is at index 6, so the distance is 1 ``` **方法 1**: * **方法**:該任務是查找兩個給定數字之間的距離,因此,使用嵌套循環查找任意兩個元素之間的距離。 用于選擇第一個元素(`x`)的外循環和用于遍歷數組以搜索另一個元素(`y`)并在它們之間采用最小距離的內循環。 * **算法**: 1. 創建一個變量`m = INT_MAX` 2. 運行一個嵌套循環,外循環從頭到尾運行(循環計數器`i`),內循環從`i + 1`到結束運行(循環計數器`j`)。 3. 如果第`i`個元素是`x`,第`j`個元素是`y`,反之亦然,則將`m`更新為`m = min(m, j-i)` 4. 打印`m`的值作為最小距離 * **實現**: ## 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); ????????????} ????????} ????} ????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 <stdio.h> #include <stdlib.h> // for abs() #include <limits.h> // for INT_MAX 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); ?????????} ?????} ???} ???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 = 3; ????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); ????????????} ????????} ????????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 = 3; ????????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); ????????????} ????????} ????} ????return $min_dist; } ????// Driver Code ????$arr = array(3, 5, 4, 2, 6, 5, 6, 6, 5, 4, 8, 3); ????$n = count($arr); ????$x = 3; ????$y = 6; ????echo "Minimum distance between ",$x, " and ",$y," is ";? ????echo minDist($arr, $n, $x, $y); // This code is contributed by anuj_67\. ?> ``` **輸出**: ``` Minimum distance between 3 and 6 is 4 ``` * **復雜度分析**: * **時間復雜度**: `O(n ^ 2)`,嵌套循環用于遍歷數組。 * **空間復雜度**:`O(1)`,不需要額外的空間。 **方法 2**: * **方法**:因此,基本方法是僅檢查`x`和`y`的連續對。 對于每個元素`x`或`y`,檢查先前出現的`x`或`y`的索引,如果先前出現的元素與當前元素不相似,則更新最小距離。 但是出現一個問題,如果一個`x`前面有另一個`x`并且在`y`前面有一個`y`,那么如何獲得線對之間的最小距離呢? 通過仔細分析,可以看到每個`x`后面緊跟著`y`,反之亦然,只能是最接近的對(最小距離),因此忽略所有其他對。 * **算法**: 1. 創建變量`prev = -1`和`m = INT_MAX` 2. 從頭到尾遍歷整個數組。 3. 如果當前元素是`x`或`y`,則`prev`不等于 -1 并且`array[prev]`不等于當前元素,則更新`m = max(current_index – prev, m)`,即找到連續對之間的距離并用它更新`m`。 4. 打印`m`的值 * *感謝 wgpshashank 提出了這種方法。* ![](https://img.kancloud.cn/e5/4b/e54b77218e0ad4c4f807fec6efbc5012_1200x738.png) **實現:** ## 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 previos 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 previos 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 previos 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 previos 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\. ?> ``` **輸出**: ``` Minimum distance between 3 and 6 is 1 ``` * **復雜度分析**: * **時間復雜度**:`O(n)`。 僅需要遍歷數組一次。 * **空間復雜度**:`O(1)`。 由于不需要額外的空間。 如果您發現上述代碼/算法有誤,請寫評論,或者找到其他解決相同問題的方法。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看