<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/elements-to-be-added-so-that-all-elements-of-a-range-are-present-in-array/](https://www.geeksforgeeks.org/elements-to-be-added-so-that-all-elements-of-a-range-are-present-in-array/) 給定大小為 N 的數組。令 A 和 B 分別為數組中的最小值和最大值。 任務是查找應將給定數組添加多少個數字,以使[A,B]范圍內的所有元素在數組中至少出現一次。 **示例**: ``` Input : arr[] = {4, 5, 3, 8, 6} Output : 1 Only 7 to be added in the list. Input : arr[] = {2, 1, 3} Output : 0 ``` **方法 1(排序)** 1-對數組進行排序。 2-比較 arr [i] == arr [i + 1] -1。 如果不是,則更新計數= arr [i + 1] -arr [i] -1。 3-返回計數。 ## C++ ```cpp // C++ program for above implementation #include <bits/stdc++.h> using namespace std; // Function to count numbers to be added int countNum(int arr[], int n) { ????int count = 0; ????// Sort the array ????sort(arr, arr + n); ????// Check if elements are consecutive ????//? or not. If not, update count ????for (int i = 0; i < n - 1; i++) ????????if (arr[i] != arr[i+1] &&? ????????????arr[i] != arr[i + 1] - 1) ????????????count += arr[i + 1] - arr[i] - 1; ????return count; } // Drivers code int main() { ????int arr[] = { 3, 5, 8, 6 }; ????int n = sizeof(arr) / sizeof(arr[0]); ????cout << countNum(arr, n) << endl; ????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>

                              哎呀哎呀视频在线观看