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++
#include <iostream>
using namespace std;
void printAboveAvg( int arr[], int n)
{
double avg = 0;
for ( int i = 0; i < n; i++)
avg += arr[i];
avg = avg / n;
for ( int i = 0; i < n; i++)
if (arr[i] > avg)
cout << arr[i] << " " ;
}
int main()
{
int arr[] = { 5, 4, 6, 9, 10 };
int a = sizeof (arr) / sizeof (arr[0]);
printAboveAvg(arr, a);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void printAboveAvg( int arr[], int n)
{
double avg = 0 ;
for ( int i = 0 ; i < n; i++)
avg += arr[i];
avg = avg / n;
for ( int i = 0 ; i < n; i++)
if (arr[i] > avg)
System.out.print(arr[i] + " " );
}
public static void main (String[] args)
{
int arr[] = { 5 , 4 , 6 , 9 , 10 };
int a = arr.length;
printAboveAvg(arr, a);
}
}
|
Python3
def printAboveAvg(arr, a):
avg = 0
for i in range (a):
avg = avg + arr[i]
avg = avg / / a
for i in range (a):
if arr[i] > avg:
print (arr[i], end = " " )
arr = [ 5 , 4 , 6 , 9 , 10 ]
a = len (arr)
printAboveAvg(arr, a)
|
PHP
<?php
function printAboveAvg( $arr , $n )
{
$avg = 0;
for ( $i = 0; $i < $n ; $i ++)
$avg += $arr [ $i ];
$avg = $avg / $n ;
for ( $i = 0; $i < $n ; $i ++)
if ( $arr [ $i ] > $avg )
echo $arr [ $i ] , " " ;
}
$arr = array (5, 4, 6, 9, 10);
$a = count ( $arr );
printAboveAvg( $arr , $a );
?>
|
C#
using System;
using System.Collections.Generic;
class GFG {
static void printAboveAvg( int []arr, int n)
{
double avg = 0;
for ( int i = 0; i < n; i++)
avg += arr[i];
avg = avg / n;
for ( int i = 0; i < n; i++)
if (arr[i] > avg)
Console.Write(arr[i] + " " );
}
public static void Main()
{
int []arr = {5, 4, 6, 9, 10};
int a = arr.Length;
printAboveAvg(arr, a);
}
}
|
Javascript
<script>
function printAboveAvg(arr, n)
{
let avg = 0;
for (let i = 0; i < n; i++)
avg += arr[i];
avg = avg / n;
for (let i = 0; i < n; i++)
if (arr[i] > avg)
document.write(arr[i] + " " );
}
let arr = [ 5, 4, 6, 9, 10 ];
let a = arr.length;
printAboveAvg(arr, a);
</script>
|
Time complexity: O(N) where N is length of the given array
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...