Skip to content
Related Articles

Related Articles

Find elements larger than half of the elements in an array
  • Difficulty Level : Basic
  • Last Updated : 25 Mar, 2021

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 6

Input  :  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 the implementation of this sorting based approach.

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




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

Javascript




<script>
 
// JavaScript 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
    arr.sort();
    
    // Print last ceil(n/2) elements
    for(let i = n - 1; i >= n / 2; i--)
        document.write(arr[i] + " "); 
}
 
// Driver Code
let arr = [1, 3, 6, 1, 0, 9];
let n = arr.length;
findLarger(arr, n);
 
// This code is contributed by chinmoy1997pal
     
</script>

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.
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :