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

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 兩個未排序數組之間的最小差值對 > 原文: [https://www.geeksforgeeks.org/smallest-difference-pair-values-two-unsorted-arrays/](https://www.geeksforgeeks.org/smallest-difference-pair-values-two-unsorted-arrays/) 給定兩個整數數組,請計算差異最小(非負)的一對值(每個數組中的一個值)。 退還差額。 **示例**: ``` Input : A[] = {l, 3, 15, 11, 2} B[] = {23, 127, 235, 19, 8} Output : 3 That is, the pair (11, 8) Input : A[] = {l0, 5, 40} B[] = {50, 90, 80} Output : 10 That is, the pair (40, 50) ``` 一種簡單的**解決方案**是使用兩個具有**時間復雜度 `O(n^2)`**的循環進行暴力破解。 **更好的解決方案**是對數組進行排序。 對數組進行排序后,我們可以使用下文中討論的方法通過遍歷數組來找到最小差異。 [從兩個排序的數組中查找最接近的對](https://www.geeksforgeeks.org/given-two-sorted-arrays-number-x-find-pair-whose-sum-closest-x/) 考慮以下兩個數組: A:{1、2、11、15} B:{4、12、19、23、127、235} **1\.** 假設一個指針 a 指向 A 的開頭,一個指針 b 指向 B 的開頭。a 和 bis 之間的當前差值 3.將其存儲為最小值。 **2\.** 我們如何(可能)縮小這種差異? 好吧,bis 處的值大于 a 處的值,因此移動 b 只會使差值更大。 因此,我們要移動一個。 **3\.** 現在 a 指向 2,b(靜止)指向 4。該差為 2,因此我們應更新 min。 移動 a,因為它較小。 **4\.** 現在 a 指向 11,b 指向 4.移動 b。 **5\.** 現在,a 指向 11,b 指向 12。將 min 更新為 1。移動 b。 等等。 以下是該想法的實現。 ## C++ ```cpp // C++ Code to find Smallest? // Difference between two Arrays #include <bits/stdc++.h> using namespace std; // function to calculate Small? // result between two arrays int findSmallestDifference(int A[], int B[], ???????????????????????????int m, int n) { ????// Sort both arrays using ????// sort function ????sort(A, A + m); ????sort(B, B + n); ????int a = 0, b = 0; ????// Initialize result as max value ????int result = INT_MAX; ????// Scan Both Arrays upto? ????// sizeof of the Arrays ????while (a < m && b < n) ????{ ????????if (abs(A[a] - B[b]) < result) ????????????result = abs(A[a] - B[b]); ????????// Move Smaller Value ????????if (A[a] < B[b]) ????????????a++; ????????else ????????????b++; ????} ????// return final sma result ????return result;? } // Driver Code int main() { ????// Input given array A ????int A[] = {1, 2, 11, 5}; ????// Input given array B ????int B[] = {4, 12, 19, 23, 127, 235}; ????// Calculate size of Both arrays ????int m = sizeof(A) / sizeof(A[0]); ????int n = sizeof(B) / sizeof(B[0]); ????// Call function to print? ????// smallest result ????cout << findSmallestDifference(A, B, m, n); ????return 0; } ```
                  <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>

                              哎呀哎呀视频在线观看