<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/check-whether-arithmetic-progression-can-formed-given-array/](https://www.geeksforgeeks.org/check-whether-arithmetic-progression-can-formed-given-array/) 給定 **n** 個整數的數組。 任務是檢查是否可以使用所有給定元素形成算術級數。 如果可能,打印“是”,否則打印“否”。 **示例**: ``` Input : arr[] = {0, 12, 4, 8} Output : Yes Rearrange given array as {0, 4, 8, 12} which forms an arithmetic progression. Input : arr[] = {12, 40, 11, 20} Output : No ``` **方法 1(簡單)** 一個簡單的解決方案是先找到最小的元素,然后找到第二個最小的元素,然后找出這兩者之間的差。 將此差設為 d。 找到差異后,找到第三最小,第四最小,依此類推。 在找到每第 i 個最小的最小值(從第 3 個開始)之后,找到當前元素的值與前一個元素的值之間的差。 如果差異與 d 不同,則返回 false。 如果所有元素都相同,則返回 true。 該解決方案的時間復雜度為 `O(n^2)` **方法 2(使用排序)** 這個想法是對給定的數組進行排序。 排序后,檢查連續元素之間的差異是否相同。 如果所有差異都相同,則可以進行算術級數運算。 以下是此方法的實現: ## C++ ```cpp // C++ program to check if a given array // can form arithmetic progression #include<bits/stdc++.h> using namespace std; // Returns true if a permutation of arr[0..n-1] // can form arithmetic progression bool checkIsAP(int arr[], int n) { ??if (n == 1) ????return true; ??// Sort array ??sort(arr, arr + n); ??// After sorting, difference between ??// consecutive elements must be same. ??int d = arr[1] - arr[0]; ??for (int i=2; i<n; i++) ????if (arr[i] - arr[i-1] != d) ??????return false; ??return true; } // Driven Program int main() { ??int arr[] = { 20, 15, 5, 0, 10 }; ??int n = sizeof(arr)/sizeof(arr[0]); ??(checkIsAP(arr, n))? (cout << "Yes" << endl) : ???????????????????????(cout << "No" << 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>

                              哎呀哎呀视频在线观看