 GeeksforGeeks App
Open App Browser
Continue

# Find elements larger than half of the elements in an array | Set 2

Given an array arr[] consisting of N positive integers, the task is to find the elements which are greater than at least half of the array elements.

Examples:

Input: arr[] = {1, 6, 3, 4}
Output: 4 6
Explanation:
Size of the array is 4. The elements which are greater than atleast N/2(= 4/2 = 2) elements of the array are 4 and 6.

Input: arr[] = {10, 4, 2, 8, 9}
Output: 10 9 8

Time Complexity: O(N2)
Auxiliary Space: O(1)

Time Complexity: O(N*log N)
Auxiliary Space: O(1)

Approach: The above approach can also be optimized by using Hashing by keeping track of the count of array elements. After finding the frequency of each element, print elements in non-increasing order till N/2 elements have been printed. Follow the steps below to solve the problem:

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach` `#include ``using` `namespace` `std;` `// Function to find the element that``// are larger than half of elements``// of the array``void` `findLarger(``int` `arr[], ``int` `n)``{``    ``// Find the value of mid``    ``int` `mid = (n + 1) / 2;` `    ``// Stores the maximum element``    ``int` `mx = *max_element(arr, arr + n);` `    ``// Stores the frequency of each``    ``// array element``    ``int` `count[mx + 1] = { 0 };``    ``for` `(``int` `i = 0; i < n; i++) {``        ``count[arr[i]]++;``    ``}` `    ``// Traverse the array in the``    ``// reverse order``    ``for` `(``int` `i = mx; i >= 0; i--) {` `        ``while` `(count[i] > 0) {` `            ``// Decrement the value``            ``// of count[i] and mid``            ``count[i]--;``            ``mid--;` `            ``// Print the current element``            ``cout << i << ``' '``;` `            ``// Check if the value of``            ``// mid is equal to 0``            ``if` `(mid == 0)``                ``break``;``        ``}``        ``if` `(mid == 0)``            ``break``;``    ``}``}` `// Driver Code``int` `main()``{``    ``int` `arr[] = { 10, 4, 2, 8, 9 };``    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr);``    ``findLarger(arr, N);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.util.*;` `class` `GFG{` `// Function to find the element that``// are larger than half of elements``// of the array``static` `void` `findLarger(``int` `arr[], ``int` `n)``{``    ``// Find the value of mid``    ``int` `mid = (n + ``1``) / ``2``;` `    ``// Stores the maximum element``    ``int` `mx = Arrays.stream(arr).max().getAsInt();` `    ``// Stores the frequency of each``    ``// array element``    ``int` `count[] = ``new` `int``[mx+``1``];``    ``for` `(``int` `i = ``0``; i < mx+``1``; i++) {``        ``count[i] = ``0``;``    ``}` `    ``for` `(``int` `i = ``0``; i < n; i++) {``        ``count[arr[i]]++;``    ``}` `    ``// Traverse the array in the``    ``// reverse order``    ``for` `(``int` `i = mx; i >= ``0``; i--) {` `        ``while` `(count[i] > ``0``) {` `            ``// Decrement the value``            ``// of count[i] and mid``            ``count[i]--;``            ``mid--;` `            ``// Print the current element``            ``System.out.print(i + ``" "``);` `            ``// Check if the value of``            ``// mid is equal to 0``            ``if` `(mid == ``0``)``                ``break``;``        ``}``        ``if` `(mid == ``0``)``            ``break``;``    ``}``}`  `// Driver Code``public` `static` `void` `main(String[] args)``{``    ``int` `arr[] = { ``10``, ``4``, ``2``, ``8``, ``9` `};``    ``int` `N = arr.length;``    ``findLarger(arr, N);``}``}` `// This code is contributed by sanjoy_62.`

## Python3

 `# Python program for the above approach` `# Function to find the element that``# are larger than half of elements``# of the array``def` `findLarger(arr, n):``    ``# Find the value of mid``    ``mid ``=` `(n ``+` `1``) ``/``/` `2` `    ``# Stores the maximum element``    ``mx ``=` `max``(arr)` `    ``# Stores the frequency of each``    ``# array element``    ``count ``=` `[``0``]``*``(mx ``+` `1``)` `    ``for` `i ``in` `range``(n):``        ``count[arr[i]] ``+``=` `1` `    ``# Traverse the array in the``    ``# reverse order``    ``for` `i ``in` `range``(mx,``-``1``, ``-``1``):``        ``while` `(count[i] > ``0``):` `            ``# Decrement the value``            ``# of count[i] and mid``            ``count[i] ``-``=` `1``            ``mid ``-``=` `1` `            ``# Print current element``            ``print``(i, end ``=` `" "``)` `            ``# Check if the value of``            ``# mid is equal to 0``            ``if` `(mid ``=``=` `0``):``                ``break``        ``if` `(mid ``=``=` `0``):``            ``break` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ``arr ``=` `[``10``, ``4``, ``2``, ``8``, ``9` `]``    ``N ``=` `len``(arr)``    ``findLarger(arr, N)` `# This code is contributed by mohit kumar 29.`

## C#

 `// C# program for the above approach``using` `System;``using` `System.Collections.Generic;` `class` `GFG{` `// Function to find the element that``// are larger than half of elements``// of the array``static` `void` `findLarger(``int` `[]arr, ``int` `n)``{``    ``// Find the value of mid``    ``int` `mid = (n + 1) / 2;` `    ``// Stores the maximum element``    ``int` `mx = Int32.MinValue;``    ``for``(``int` `i=0;imx)``            ``mx = arr[i];``    ``}` `    ``// Stores the frequency of each``    ``// array element``    ``int` `[]count = ``new` `int``[mx + 1];``    ``Array.Clear(count,0,mx+1);``    ``for` `(``int` `i = 0; i < n; i++) {``        ``count[arr[i]]++;``    ``}` `    ``// Traverse the array in the``    ``// reverse order``    ``for` `(``int` `i = mx; i >= 0; i--) {` `        ``while` `(count[i] > 0) {` `            ``// Decrement the value``            ``// of count[i] and mid``            ``count[i]--;``            ``mid--;` `            ``// Print the current element``            ``Console.Write(i+``" "``);` `            ``// Check if the value of``            ``// mid is equal to 0``            ``if` `(mid == 0)``                ``break``;``        ``}``        ``if` `(mid == 0)``            ``break``;``    ``}``}` `// Driver Code``public` `static` `void` `Main()``{``    ``int` `[]arr = { 10, 4, 2, 8, 9 };``    ``int` `N = arr.Length;``    ``findLarger(arr, N);``}``}` `// This code is contributed by SURENDRA_GANGWAR.`

## Javascript

 ``

Output:

`10 9 8`

Time Complexity: O(max(M2, N)), where M is the maximum element of the array arr[] and N is the length of the given array.
Auxiliary Space: O(M)

My Personal Notes arrow_drop_up