Find array elements that are greater than average - GeeksforGeeks

28 Apr, 2021
Given an array of numbers, print all those elements that are greater than average.
Examples: 
Input : 5, 4, 6, 9, 10
Output : 9 10
Explanation:
avg = 5 + 4 + 6 + 9 + 10 / 5;
avg = 34 / 5
avg = 6.8
Elements greater than 6.8 are 9 and
10

Input : 1, 2, 4, 0, 5
Output : 4 5
1) Find average of elements. 2) Traverse array again and print elements that are greater than average, 
C++
Java
Python3
PHP
C#
Javascript
// A C++ program to print elements which are// greater than avg of array#include <iostream>using namespace std; // Print array elements greater than averagevoid printAboveAvg(int arr[], int n){    // Find average    double avg = 0;    for (int i = 0; i < n; i++)        avg += arr[i];       avg = avg / n;     // Print elements greater than average    for (int i = 0; i < n; i++)        if (arr[i] > avg)            cout << arr[i] << " ";} // Driver programint main(){    int arr[] = { 5, 4, 6, 9, 10 };    int a = sizeof(arr) / sizeof(arr[0]);    printAboveAvg(arr, a);    return 0;}
// A Java program to print elements which are// greater than avg of arrayimport java.io.*; class GFG {     // Print array elements greater than average    static void printAboveAvg(int arr[], int n)    {                 // Find average        double avg = 0;        for (int i = 0; i < n; i++)            avg += arr[i];        avg = avg / n;             // Print elements greater than average        for (int i = 0; i < n; i++)            if (arr[i] > avg)                System.out.print(arr[i] + " ");    }     // Driver program    public static void main (String[] args)    {        int arr[] = { 5, 4, 6, 9, 10 };        int a = arr.length;        printAboveAvg(arr, a);         }} // This code is contributed by anuj_67.
# python program to print elements# which are greater than avg of# array # Print array elements greater# than averagedef printAboveAvg(arr, a):     # Find average    avg = 0    for i in range(a):        avg = avg + arr[i]             avg = avg // a     # Print elements greater than    # average    for i in range(a):        if arr[i] > avg:            print(arr[i], end = " ") # Driver Programarr = [5, 4, 6, 9, 10]a = len(arr)printAboveAvg(arr, a) # This code is contributed# by Shrikant13.
<?php// A PHP program to print// elements which are// greater than avg of array // Print array elements// greater than averagefunction printAboveAvg( $arr, $n){         // Find average    $avg = 0;         for ($i = 0; $i < $n; $i++)        $avg += $arr[$i];    $avg = $avg / $n;     // Print elements greater    // than average    for ($i = 0; $i < $n; $i++)        if ($arr[$i] > $avg)            echo $arr[$i] , " ";}     // Driver Code    $arr = array(5, 4, 6, 9, 10);    $a = count($arr);    printAboveAvg($arr, $a); // This code is contributed by anuj_67.?>
// A C# program to print elements which are// greater than avg of arrayusing System;using System.Collections.Generic; class GFG {         // Print array elements    // greater than average    static void printAboveAvg(int []arr, int n)    {        // Find average        double avg = 0;        for (int i = 0; i < n; i++)            avg += arr[i];        avg = avg / n;             // Print elements greater        // than average        for (int i = 0; i < n; i++)            if (arr[i] > avg)                Console.Write(arr[i] + " ");    }         // Driver Code    public static void Main()    {        int []arr = {5, 4, 6, 9, 10};        int a = arr.Length;        printAboveAvg(arr, a);    }} // This code is contributed by// Manish Shaw (manishshaw1)
<script> // A Javascript program to print// elements which are greater// than avg of array // Print array elements greater// than averagefunction printAboveAvg(arr, n){         // Find average    let avg = 0;    for(let i = 0; i < n; i++)        avg += arr[i];                avg = avg / n;     // Print elements greater than average    for(let i = 0; i < n; i++)        if (arr[i] > avg)            document.write(arr[i] + " ");} // Driver codelet arr = [ 5, 4, 6, 9, 10 ];let a = arr.length; printAboveAvg(arr, a);     // This code is contributed by jana_sayantan </script>
9 10
 
shrikanth13
vt_m
manishshaw1
jana_sayantan
Articles
School Programming
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Time Complexity and Space Complexity
Docker - COPY Instruction
SQL | Date functions
Time complexities of different data structures
Difference between Class and Object
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
Interfaces in Java