Find array elements that are greater than average
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++
// A C++ program to print elements which are // greater than avg of array #include <iostream> using namespace std; // Print array elements greater than average 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) cout << arr[i] << " "; } // Driver program int main() { int arr[] = { 5, 4, 6, 9, 10 }; int a = sizeof(arr) / sizeof(arr[0]); printAboveAvg(arr, a); return 0; } |
chevron_right
filter_none
Java
// A Java program to print elements which are // greater than avg of array import 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. |
chevron_right
filter_none
Python3
# python program to print elements # which are greater than avg of # array # Print array elements greater # than average def 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 Program arr = [5, 4, 6, 9, 10] a = len(arr) printAboveAvg(arr, a) # This code is contributed # by Shrikant13. |
chevron_right
filter_none
PHP
<?php // A PHP program to print // elements which are // greater than avg of array // Print array elements // greater than average function 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. ?> |
chevron_right
filter_none
C#
// A C# program to print elements which are // greater than avg of array using 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) |
chevron_right
filter_none
Output:
9 10
Recommended Posts:
- Find the first, second and third minimum elements in an array
- Program to find sum of elements in a given array
- Average numbers in array
- Find the average of first N natural numbers
- Find the Next perfect square greater than a given number
- Replace array elements by sum of next two consecutive elements
- Divide every element of one array by other array elements
- Sum of consecutive two elements in a array
- Sum of array elements using recursion
- Sum of array Elements without using loops and recursion
- Program to print Sum of even and odd elements in an array
- Count number of even and odd elements in an array
- Program for multiplication of array elements
- Product of all pairwise consecutive elements in an Array
- Count the number of elements in an array which are divisible by k
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



