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

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 數組中不同的相鄰元素 > 原文: [https://www.geeksforgeeks.org/distinct-adjacent-elements-array/](https://www.geeksforgeeks.org/distinct-adjacent-elements-array/) 給定一個數組,找出是否有可能通過交換兩個相鄰的數組元素來獲得具有不同的相鄰元素的數組。 例子: ``` Input : 1 1 2 Output : YES swap 1 (second last element) and 2 (last element), to obtain 1 2 1, which has distinct neighbouring elements . Input : 7 7 7 7 Output : NO We can't swap to obtain distinct elements in neighbor . ``` 為了獲得具有不同的相鄰元素的數組,只有在最常出現的元素的頻率小于或等于數組大小的一半(即`<= (n + 1) / 2)`時才可能使用。為了更清楚地考慮不同的示例: 第一個例子:`a[] = {1, 1, 2, 3, 1}` 我們可以通過交換 元素(第二和第三)和 獲得數組`{1, 2, 1, 3, 1}`數組`a`中的(第 4 和第 5)元素。 此處 1 出現最多,其頻率為 `3 (6 + 1) / 2`。 因此,它將永遠不可能。 下面是這種方法的實現。 ## C++ ```cpp // C++ program to check if we can make // neighbors distinct. #include <bits/stdc++.h> using namespace std; void distinctAdjacentElement(int a[], int n) { ????// map used to count the frequency ????// of each element occurring in the ????// array ????map<int, int> m; ????// In this loop we count the frequency ????// of element through map m . ????for (int i = 0; i < n; ++i) ????????m[a[i]]++; ????// mx store the frequency of element which ????// occurs most in array . ????int mx = 0; ????// In this loop we calculate the maximum ????// frequency and store it in variable mx. ????for (int i = 0; i < n; ++i) ????????if (mx < m[a[i]]) ????????????mx = m[a[i]]; ????// By swapping we can adjust array only ????// when the frequency of the element ????// which occurs most is less than or ????// equal to (n + 1)/2 . ????if (mx > (n + 1) / 2) ????????cout << "NO" << endl; ????else ????????cout << "YES" << endl; } // Driver program to test the above function int main() { ????int a[] = { 7, 7, 7, 7 }; ????int n = sizeof(a) / sizeof(a[0]); ????distinctAdjacentElement(a, 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>

                              哎呀哎呀视频在线观看