Find array elements that are greater than average
Last Updated :
03 Mar, 2023
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,
Algorithm:
Step 1: Start
Step 2: create a static function of void return type name it as printAboveAvg which take an array and its length as input value.
Step 3: initialize a variable of double data type name it as avg to 0.
Step 4: start a for loop and traverse from the given array.
a. add value of every ith index to "avg" and assign it to "avg".
b. Divide 'avg' by the size of the array to get the average value.
Step 5: start another for loop and traverse through array.
a. now if value of array at the index i is greater than "avg" then print that value.
Step 6: End
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;
}
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.
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.
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.
?>
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)
JavaScript
<script>
// A Javascript program to print
// elements which are greater
// than avg of array
// Print array elements greater
// than average
function 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 code
let arr = [ 5, 4, 6, 9, 10 ];
let a = arr.length;
printAboveAvg(arr, a);
// This code is contributed by jana_sayantan
</script>
Time complexity: O(N) where N is length of the given array
Auxiliary Space: O(1)
Similar Reads
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 6Explanation:Size of the array is 4. The elements which are greater than atleast N/2(= 4/2 = 2) elements
7 min read
Find all indices of Array having value same as average of other elements Given an array arr[] of N integers, the task is to find all indices in the array, such that for each index i the arithmetic mean of all elements except arr[i] is equal to the value of the element at that index. Examples : Input: N = 5, arr[] = {1, 2, 3, 4, 5}Output : {2}Explanation : Upon removing a
6 min read
Count occurrences of the average of array elements with a given number Given an array of N integers and an integer x . For every integer of the array a[i], the task is to calculate the count of numbers in the array with value equals to the average of element a[i] and x. That is, the number of occurrences of the (average of element a[i] and x) in the array. Examples: In
7 min read
Count of pairs of Array elements with average at least K Given an array A[] of size N consisting of N integers, the task is to count the number of pairs such that their average is greater or equal to K. Example: Input: N = 4, K = 3, A = {5, 1, 3, 4}Output: 4Explanation: (5, 1), (5, 3), (5, 4) and (3, 4) are the required pairs with average greater or equal
11 min read
Find the deleted value from the array when average of original elements is given Given an array of length N + K. Also given the average avg of all the elements of the array. If an element that appears exactly K time got removed from the array (all the occurrences) and the resultant array is given, the task is to find the element X. Note that if X is not an integer then print -1.
5 min read