<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 圓形數組中相鄰元素的最小絕對差 > 原文: [https://www.geeksforgeeks.org/minimum-absolute-difference-adjacent-elements-circular-array/](https://www.geeksforgeeks.org/minimum-absolute-difference-adjacent-elements-circular-array/) 給定 n 個整數,它們形成一個圓。 找到任何相鄰對的最小絕對值。 如果有許多最佳解決方案,請輸出其中任何一個。 注意:它們在圈子中 例子: ``` Input : arr[] = {10, 12, 13, 15, 10} Output : 0 Explanation: |10 - 10| = 0 which is the minimum possible. Input : arr[] = {10, 20, 30, 40} Output : 10 Explanation: |10 - 20| = 10 which is the minimum, 2 3 or 3 4 can be the answers also. ``` 首先考慮最小值是第一和第二元素。 從第二個元素遍歷到最后一個。 檢查每個相鄰對的差異并存儲最小值。 當到達最后一個元素時,檢查其與第一個元素的區別。 下面是上述方法的實現。 ## C++ ```cpp // C++ program to find maximum difference // between adjacent elements in a circular array. #include <bits/stdc++.h> using namespace std; void minAdjDifference(int arr[], int n) { ????if (n < 2) ????????return; ????// Checking normal adjacent elements ????int res = abs(arr[1] - arr[0]); ????for (int i = 2; i < n; i++) ????????res = min(res, abs(arr[i] - arr[i - 1])); ????// Checking circular link ????res = min(res, abs(arr[n - 1] - arr[0])); ????cout << "Min Difference = " << res; } // driver program to check the above function int main() { ????int a[] = { 10, 12, 13, 15, 10 }; ????int n = sizeof(a) / sizeof(a[0]); ????minAdjDifference(a, n); ????return 0; } ``` ## Java ```java // Java program to find maximum difference // between adjacent elements in a circular // array. class GFG { ????static void minAdjDifference(int arr[], int n) ????{ ????????if (n < 2) ????????????return; ????????// Checking normal adjacent elements ????????int res = Math.abs(arr[1] - arr[0]); ????????for (int i = 2; i < n; i++) ????????????res = Math.min(res, Math.abs(arr[i] - arr[i - 1])); ????????// Checking circular link ????????res = Math.min(res, Math.abs(arr[n - 1] - arr[0])); ????????System.out.print("Min Difference = " + res); ????} ????// driver code ????public static void main(String arg[]) ????{ ????????int a[] = { 10, 12, 13, 15, 10 }; ????????int n = a.length; ????????minAdjDifference(a, n); ????} } // This code is contributed by Anant Agarwal // and improved by Anuj Sharma. ``` ## Python3 ```py # Python3 program to find maximum? # difference between adjacent # elements in a circular array. def minAdjDifference(arr, n): ????if (n < 2): return ????# Checking normal adjacent elements ????res = abs(arr[1] - arr[0]) ????for i in range(2, n): ????????res = min(res, abs(arr[i] - arr[i - 1])) ????# Checking circular link ????res = min(res, abs(arr[n - 1] - arr[0]))? ????print("Min Difference = ", res) # Driver Code a = [10, 12, 13, 15, 10] n = len(a) minAdjDifference(a, n)? # This code is contributed by Anant Agarwal? # and improved by Anuj Sharma. ``` ## C# ```cs // C# program to find maximum difference // between adjacent elements in a circular array. using System; class GFG { ????static void minAdjDifference(int[] arr, int n) ????{ ????????if (n < 2) ????????????return; ????????// Checking normal adjacent elements ????????int res = Math.Abs(arr[1] - arr[0]); ????????for (int i = 2; i < n; i++) ????????????res = Math.Min(res, Math.Abs(arr[i] - arr[i - 1])); ????????// Checking circular link ????????res = Math.Min(res, Math.Abs(arr[n - 1] - arr[0])); ????????Console.Write("Min Difference = " + res); ????} ????// driver code ????public static void Main() ????{ ????????int[] a = { 10, 12, 13, 15, 10 }; ????????int n = a.Length; ????????minAdjDifference(a, n); ????} } // This code is contributed by Anant Agarwal // and improved by Anuj Sharma. ``` ## PHP ```php <?php // PHP program to find maximum // difference between adjacent // elements in a circular array. function minAdjDifference($arr, $n) { ????if ($n < 2) ????????return; ????// Checking normal? ????// adjacent elements ????$res = abs($arr[1] - $arr[0]); ????for ($i = 2; $i < $n; $i++) ????????$res = min($res,? ???????????????abs($arr[$i] -? ???????????????$arr[$i - 1])); ????// Checking circular link ????$res = min($res, abs($arr[$n - 1] -? ?????????????????????$arr[0]));? ????echo "Min Difference = ", $res; } // Driver Code $a = array(10, 12, 13, 15, 10); $n = count($a); minAdjDifference($a, $n);? //This code is contributed by anuj_67? //and improved by Anuj Sharma. ?> ``` Output: ``` Min Difference = 0 ``` **時間復雜度**:`O(n)` 本文由 [**努力者**](https://www.facebook.com/raja.vikramaditya.7) 貢獻。 如果您喜歡 GeeksforGeeks 并希望做出貢獻,則還可以使用 [tribution.geeksforgeeks.org](http://www.contribute.geeksforgeeks.org) 撰寫文章,或將您的文章郵寄至 tribution@geeksforgeeks.org。 查看您的文章出現在 GeeksforGeeks 主頁上,并幫助其他 Geeks。
                  <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>

                              哎呀哎呀视频在线观看