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

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 在未排序的數組中找到最大的對和 > 原文: [https://www.geeksforgeeks.org/find-the-largest-pair-sum-in-an-unsorted-array/](https://www.geeksforgeeks.org/find-the-largest-pair-sum-in-an-unsorted-array/) 給定一個未排序的不同整數,在其中找到最大的對和。 例如,`{12, 34, 10, 6, 40}`中最大的對和為 74。 難度等級:新秀 預期時間復雜度:`O(n)`【僅允許遍歷數組一次】 這個問題主要歸結為找到數組中最大和第二大元素。 通過遍歷數組一次,我們可以找到`O(n)`時間中最大和第二大的值。 ``` 1) Initialize both first and second largest first = max(arr[0], arr[1]) second = min(arr[0], arr[1]) 2) Loop through remaining elements (from 3rd to end) a) If the current element is greater than first, then update first and second. b) Else if the current element is greater than second then update second 3) Return (first + second) ``` 下面是上述算法的實現: ## C++ ```cpp // C++ program to find largest pair sum in a given array #include<iostream> using namespace std; /* Function to return largest pair sum. Assumes that? ???there are at-least? two elements in arr[] */ int findLargestSumPair(int arr[], int n) { ????// Initialize first and second largest element ????int first, second; ????if (arr[0] > arr[1]) ????{ ????????first = arr[0]; ????????second = arr[1]; ????} ????else ????{ ????????first = arr[1]; ????????second = arr[0]; ????} ????// Traverse remaining array and find first and second largest ????// elements in overall array ????for (int i = 2; i<n; i ++) ????{ ????????/* If current element is greater than first then update both ??????????first and second */ ????????if (arr[i] > first) ????????{ ????????????second = first; ????????????first = arr[i]; ????????} ????????/* If arr[i] is in between first and second then update second? */ ????????else if (arr[i] > second && arr[i] != first) ????????????second = arr[i]; ????} ????return (first + second); } /* Driver program to test above function */ int main() { ????int arr[] = {12, 34, 10, 6, 40}; ????int n = sizeof(arr)/sizeof(arr[0]); ????cout << "Max Pair Sum is " << findLargestSumPair(arr, 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>

                              哎呀哎呀视频在线观看