# Find elements larger than half of the elements in an array

Given an array of n elements, the task is to find the elements that are greater than half of elements in an array. In case of odd elements, we need to print elements larger than floor(n/2) elements where n is total number of elements in array.

**Examples :**

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

A **naive approach** is to take an element and compare with all other elements and if it is greater then increment the count and then check if count is greater than n/2 elements then print.

An **efficient method** is to sort the array in ascending order and then print last ceil(n/2) elements from sorted array.

Below is C++ implementation of this sorting based approach.

## 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; ` `} ` |

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

## C#

`// 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. ` |

*chevron_right*

*filter_none*

## 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. ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

9 6 3

This article is contributed by **Sahil Chhabra (KILLER)**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Find the XOR of first half and second half elements of an array
- Find the sum of the first half and second half elements of an array
- Minimizing array sum by subtracting larger elements from smaller ones
- Find Kth element in an array containing odd elements first and then even elements
- Find all elements in array which have at-least two greater elements
- Find elements of array using XOR of consecutive elements
- Find prime numbers in the first half and second half of an array
- Maximum difference between two elements such that larger element appears after the smaller number
- Maximum sum such that exactly half of the elements are selected and no two adjacent
- Check if Array elements can be maximized upto M by adding all elements from another array
- Find original array from encrypted array (An array of sums of other elements)
- Find last two remaining elements after removing median of any 3 consecutive elements repeatedly
- Sort elements of an array in increasing order of absolute difference of adjacent elements
- Average of remaining elements after removing K largest and K smallest elements from array
- Find minimum value to assign all array elements so that array product becomes greater
- Reduce the array by deleting elements which are greater than all elements to its left
- Maximize sum of atmost K elements in array by taking only corner elements | Set 2
- Count of Array elements to be divided by 2 to make at least K elements equal
- Length of array pair formed where one contains all distinct elements and other all same elements
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum