Program for weighted mean of natural numbers.
There are given an array of natural numbers and another array with corresponding weight of the number. Then we have to calculate the weighted mean.
Where x (bar) is called the weighted mean, x[i] is the elements of array, and W[i] is the weight of elements of array x[i].
Examples:
Input :
X[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
W[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
weighted mean
= (W[0] * X[0] + W[1] * X[1] + W[2] * X[2] + . . . +
W[n-1] * X[n-1]) / (W[0] + W[1] + W[2] + . . . + W[n-1])
= (1 * 1 + 2 * 2 + 3 * 3 + . . . + 10 * 10) /
(1 + 2 + 3 + . . . + 10)
= 385 / 55 = 7
Output : 7
Input :
X[] = {3, 4, 5, 6, 7}
W[] = {4, 5, 6, 7, 8}
weighted mean
= (W[0] * X[0] + W[1] * X[1] + W[2] * X[2] + . . . +
W[n-1] * X[n-1]) / (W[0] + W[1] + W[2] + . . . + W[n-1])
= (3 * 4 + 4 * 5 + 5 * 6 + 6 * 7 + 7 * 8) /
(4 + 5 + 6 + 7 + 8)
= 160 / 30 = 5.33333
Output : 5.33333
Implementation: A simple solution to solve weighed mean.
C++
#include<bits/stdc++.h>
using namespace std;
float weightedMean( int X[], int W[], int n)
{
int sum = 0, numWeight = 0;
for ( int i = 0; i < n; i++)
{
numWeight = numWeight + X[i] * W[i];
sum = sum + W[i];
}
return ( float )numWeight / sum;
}
int main()
{
int X[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int W[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int n = sizeof (X)/ sizeof (X[0]);
int m = sizeof (W)/ sizeof (W[0]);
if (n == m)
cout << weightedMean(X, W, n);
else
cout << "-1" ;
return 0;
}
|
Java
class GFG {
static float weightedMean( int X[], int W[],
int n)
{
int sum = 0 , numWeight = 0 ;
for ( int i = 0 ; i < n; i++)
{
numWeight = numWeight + X[i] * W[i];
sum = sum + W[i];
}
return ( float )(numWeight) / sum;
}
public static void main(String args[])
{
int X[] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 };
int W[] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 };
int n = X.length;
int m = W.length;
if (n == m)
System.out.println(weightedMean(X, W, n));
else
System.out.println( "-1" );
}
}
|
Python
def weightedMean(X,W,n) :
sum = 0
numWeight = 0
i = 0
while i < n :
numWeight = numWeight + X[i] * W[i]
sum = sum + W[i]
i = i + 1
return ( float )(numWeight / sum )
X = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]
W = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]
n = len (X)
m = len (W)
if (n = = m) :
print weightedMean(X, W, n)
else :
print "-1"
|
C#
using System;
class GFG {
static float weightedMean( int []X, int []W,
int n)
{
int sum = 0, numWeight = 0;
for ( int i = 0; i < n; i++)
{
numWeight = numWeight + X[i] * W[i];
sum = sum + W[i];
}
return ( float )(numWeight) / sum;
}
public static void Main()
{
int []X = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int []W = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int n = X.Length;
int m = W.Length;
if (n == m)
Console.WriteLine(weightedMean(X, W, n));
else
Console.WriteLine( "-1" );
}
}
|
PHP
<?php
function weightedMean( $X , $W , $n )
{
$sum = 0; $numWeight = 0;
for ( $i = 0; $i < $n ; $i ++)
{
$numWeight = $numWeight +
$X [ $i ] * $W [ $i ];
$sum = $sum + $W [ $i ];
}
return (float)( $numWeight / $sum );
}
$X = array (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
$W = array (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
$n = sizeof( $X );
$m = sizeof( $W );
if ( $n == $m )
echo (weightedMean( $X , $W , $n ));
else
echo ( "-1" );
?>
|
Javascript
<script>
function weightedMean(X, W, n)
{
let sum = 0, numWeight = 0;
for (let i = 0; i < n; i++)
{
numWeight = numWeight + X[i] * W[i];
sum = sum + W[i];
}
return (numWeight) / sum;
}
let X = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];
let W = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];
let n = X.length;
let m = W.length;
if (n == m)
document.write(weightedMean(X, W, n));
else
document.write( "-1" );
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Second method – to compute the weighted mean of first n natural numbers. It is the formula to compute the weighted mean of first n natural numbers. In this method, we have given first n natural number and their weight are also be the natural numbers. Then we generate the formula
Weighted Mean
= (W[0] * X[0] + W[1] * X[1] + W[2] * X[2] + . . . +
W[n-1] * X[n-1]) / (W[0] + W[1] + W[2] + . . . + W[n-1])
= (1 * 1 + 2 * 2 + 3 * 3 + . . . + n * n) / (1 + 2 + 3 + . . . + n)
= (n * (n + 1) * (2 * n + 1) / 6) / (n * (n + 1) / 2)
Weighted Mean = (2 * n + 1) / 3
Example: Weighted mean of first 10 natural numbers
n = 10
Weighted mean
= (2 * 10 + 1) / 3 = 21 / 3 = 7
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
int weightedMean( int n)
{
return (2 * n + 1)/3;
}
int main()
{
int n = 10;
cout << weightedMean(n);
return 0;
}
|
Java
import java.io.*;
public class GFG {
static int weightedMean( int n)
{
return ( 2 * n + 1 )/ 3 ;
}
static public void main (String[] args)
{
int n = 10 ;
System.out.println(weightedMean(n));
}
}
|
Python3
def weightedMean(n):
return ( 2 * n + 1 ) / 3
n = 10
print ( int (weightedMean(n)))
|
C#
using System;
public class GFG {
static int weightedMean( int n)
{
return (2 * n + 1) / 3;
}
static public void Main ()
{
int n = 10;
Console.WriteLine(weightedMean(n));
}
}
|
PHP
<?php
function weightedMean( $n )
{
return (2 * $n + 1) / 3;
}
$n = 10;
echo (weightedMean( $n ));
?>
|
Javascript
<script>
function weightedMean(n)
{
return parseInt((2 * n + 1) / 3, 10);
}
let n = 10;
document.write(weightedMean(n));
</script>
|
Time complexity: O(1) as it is performing constant operations
Auxiliary space: O(1)
Last Updated :
11 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...