<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/find-elements-larger-half-elements-array/](https://www.geeksforgeeks.org/find-elements-larger-half-elements-array/) 給定 n 個元素的數組,任務是查找大于數組中元素一半的元素。 對于奇數元素,我們需要打印大于 floor(n / 2)個元素,其中 n 是數組中元素的總數。 **示例**: ``` Input : arr[] = {1, 6, 3, 4} Output : 4 6 Input : arr[] = {10, 4, 2, 8, 9} Output : 10 9 8 ``` **樸素的方法**是獲取一個元素并將其與所有其他元素進行比較,如果大于,則增加計數,然后檢查計數是否大于 n / 2 個元素,然后打印。 **有效方法**是按升序對數組進行排序,然后從排序后的數組中打印最后的 ceil(n / 2)個元素。 下面是這種基于排序的方法的 C++ 實現。 ## C/C++ ``` // C++ program to find elements that are larger than // half of the elements in array #include <bits/stdc++.h> using namespace std; // Prints elements larger than n/2 element void findLarger(int arr[], int n) { ????// Sort the array in ascending order ????sort(arr, arr + n); ????// Print last ceil(n/2) elements ????for (int i = n-1; i >= n/2; i--) ????????cout << arr[i] << " ";???? } // Driver program? int main()? { ????int arr[] = {1, 3, 6, 1, 0, 9}; ????int n = sizeof(arr)/sizeof(arr[0]); ????findLarger(arr, n); ????return 0; } ``` ## Java ```java // Java program to find elements that are? // larger than half of the elements in array import java.util.*; class Gfg { ????// Prints elements larger than n/2 element ????static void findLarger(int arr[], int n) ????{ ????????// Sort the array in ascending order ????????Arrays.sort(arr); ????????// Print last ceil(n/2) elements ????????for (int i = n-1; i >= n/2; i--) ????????????System.out.print(arr[i] + " ");?? ????} ????// Driver program? ????public static void main(String[] args)? ????{ ????????int arr[] = {1, 3, 6, 1, 0, 9}; ????????int n = arr.length; ????????findLarger(arr, n); ????}???? } // This code is contributed by Raghav Sharma ``` ## Python ``` # Python program to find elements that are larger than # half of the elements in array # Prints elements larger than n/2 element def findLarger(arr,n): ????# Sort the array in ascending order ????x = sorted(arr) ????# Print last ceil(n/2) elements ????for i in range(n/2,n): ????????print(x[i]), # Driver program arr = [1, 3, 6, 1, 0, 9] n = len(arr); findLarger(arr,n) # This code is contributed by Afzal Ansari ``` ## C# ```cs // C# program to find elements? // that are larger than half? // of the elements in array using System; class GFG { ????// Prints elements larger ????// than n/2 element ????static void findLarger(int []arr,? ???????????????????????????int n) ????{ ????????// Sort the array? ????????// in ascending order ????????Array.Sort(arr); ????????// Print last ceil(n/2) elements ????????for (int i = n - 1; i >= n / 2; i--) ????????????Console.Write(arr[i] + " ");? ????} ????// Driver Code? ????public static void Main()? ????{ ????????int []arr = {1, 3, 6, 1, 0, 9}; ????????int n = arr.Length; ????????findLarger(arr, n); ????}? } // This code is contributed // by nitin mittal. ``` ## PHP ```php <?php // PHP program to find elements // that are larger than half of // the elements in array // Prints elements larger // than n/2 element function findLarger($arr, $n) { ????// Sort the array in? ????// ascending order ????sort($arr); ????// Print last ceil(n/2) elements ????for ($i = $n - 1; $i >= $n / 2; $i--) ????????echo $arr[$i] , " ";? } // Driver Code? $arr = array(1, 3, 6, 1, 0, 9); $n = count($arr); findLarger($arr, $n); // This code is contributed by anuj_67\. ?> ``` **輸出**: ``` 9 6 3 ``` 本文由 [**Sahil Chhabra(KILLER)**](https://www.facebook.com/sahil.chhabra.965) 貢獻。 如果您喜歡 GeeksforGeeks 并希望做出貢獻,則還可以使用 [tribution.geeksforgeeks.org](http://www.contribute.geeksforgeeks.org) 撰寫文章,或將您的文章郵寄至 tribution@geeksforgeeks.org。 查看您的文章出現在 GeeksforGeeks 主頁上,并幫助其他 Geeks。
                  <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>

                              哎呀哎呀视频在线观看