<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之旅 廣告
                # 在頻率大于或等于 n / 2 的排序數組中查找元素。 > 原文: [https://www.geeksforgeeks.org/find-element-sorted-array-whose-frequency-greater-equal-n2/](https://www.geeksforgeeks.org/find-element-sorted-array-whose-frequency-greater-equal-n2/) 給定長度為 n 的排序數組,請在數組中查找出現大于或等于 n / 2 次的數字。 假定這樣的元素總是存在的。 例子: ``` Input : 2 3 3 4 Output : 3 Input : 3 4 5 5 5 Output : 5 Input : 1 1 1 2 3 Output : 1 ``` 為了找到該數字,我們遍歷數組,并檢查數組中每個元素的頻率是否大于或等于 n / 2,但它需要額外的空間,并且時間復雜度為`O(n)`。 但是我們可以看到,如果在排序數組中存在大于或等于 n / 2 次的數字,則該數字必須出現在位置 n / 2,即 a [n / 2]。 ## C++ ```cpp // C++ code to find majority element in a // sorted array #include <iostream> using namespace std; int findMajority(int arr[], int n) { ????return arr[n / 2]; } int main() { ????int arr[] = { 1, 2, 2, 3 }; ????int n = sizeof(arr) / sizeof(arr[0]); ????cout << findMajority(arr, n); ????return 0; } ``` ## Java ```java // Java code to find majority element in a // sorted array public class Test { ????public static int findMajority(int arr[], int n) ????{ ????????return arr[n / 2]; ????} ????public static void main(String args[]) ????{ ????????int arr[] = { 1, 2, 2, 3 }; ????????int n = arr.length; ????????System.out.println(findMajority(arr, n)); ????} } ``` ## Python 3 ``` # Python 3 code to find # majority element in a # sorted array def findMajority(arr, n): ????return arr[int(n / 2)] # Driver Code arr = [1, 2, 2, 3] n = len(arr)? print(findMajority(arr, n)) # This code is contributed by Smitha. ``` ## C# ```cs // C# code to find majority element in a // sorted array using System; public class GFG { ????public static int findMajority(int []arr, int n) ????{ ????????return arr[n / 2]; ????} ????// Driver code ????public static void Main() ????{ ????????int []arr = { 1, 2, 2, 3 }; ????????int n = arr.Length; ????????Console.WriteLine(findMajority(arr, n)); ????} } // This code is contributed by vt_m.? ``` ## PHP ```php <?php // PHP code to find majority? // element in a sorted array function findMajority($arr, $n) { ????return $arr[intval($n / 2)]; } ????// Driver Code ????$arr = array(1, 2, 2, 3); ????$n = count($arr); ????echo findMajority($arr, $n);????? // This code is contributed by Sam007 ?> ``` Output: ``` 2 ``` 時間復雜度:`O(1)` **相關文章**: [未排序數組中的多數元素](https://www.geeksforgeeks.org/majority-element/) [檢查排序數組中的多數元素](https://www.geeksforgeeks.org/check-for-majority-element-in-a-sorted-array/)
                  <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>

                              哎呀哎呀视频在线观看