<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國際加速解決方案。 廣告
                # 查找給定數組中缺少的前 k 個自然數 > 原文: [https://www.geeksforgeeks.org/find-first-k-natural-numbers-missing-given-array/](https://www.geeksforgeeks.org/find-first-k-natural-numbers-missing-given-array/) 給定大小為 n 且數字為 k 的數組,我們需要打印給定數組中不存在的前 k 個自然數。 **示例**: ``` Input : [2 3 4] k = 3 Output : [1 5 6] Input : [-2 -3 4] k = 2 Output : [1 2] ``` 1)對給定的數組進行排序。 2)排序后,我們找到數組中第一個正數的位置。 3)現在,我們遍歷數組并將打印元素保持在兩個連續的數組元素之間的間隙中。 4)如果空格不能覆蓋 k 個缺失的數字,我們將打印大于最大數組元素的數字。 ## C++ ```cpp // C++ program to find missing k numbers // in an array. #include <bits/stdc++.h> using namespace std; // Prints first k natural numbers in // arr[0..n-1] void printKMissing(int arr[], int n, int k) { ????sort(arr, arr + n); ????// Find first positive number ????int i = 0; ????while (i < n && arr[i] <= 0) ????????i++; ????// Now find missing numbers ????// between array elements ????int count = 0, curr = 1; ????while (count < k && i < n) { ????????if (arr[i] != curr) { ????????????cout << curr << " "; ????????????count++; ????????} ????????else ????????????i++; ????????curr++; ????} ????// Find missing numbers after ????// maximum. ????while (count < k) { ????????cout << curr << " "; ????????curr++; ????????count++; ????} } // Driver code int main() { ????int arr[] = { 2, 3, 4 }; ????int n = sizeof(arr) / sizeof(arr[0]); ????int k = 3; ????printKMissing(arr, n, k); ????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>

                              哎呀哎呀视频在线观看