Given N and Standard Deviation, find N elements
Last Updated :
08 Jun, 2022
Given N and Standard deviation find the N elements.
Mean is average of element.
Mean of arr[0..n-1] = ?(arr[i]) / n
where 0 <= i < n
Variance is sum of squared differences from the mean divided by number of elements.
Variance = ?(arr[i] – mean)2 / n
Standard Deviation is square root of variance
Standard Deviation = ?(variance)
Please refer Mean, Variance and Standard Deviation for details.
Examples:
Input: 6 0
Output: 0 0 0 0 0 0
Explanation:
The standard deviation of 0, 0, 0, 0, 0, 0 is 0.
Also the standard deviation of 4, 4, 4, 4, 4, 4
is 0, we print any of the possible N elements.
Input: 3 3
Output: 0 -3.67423 3.67423
Explanation:
On calculating SD of these N elements,
we get standard deviation to be 3.
Approach:
If we look at the formula, we have two unknown terms one is xi and the other is mean. The main motive is to make the mean 0 so that we can get the formula for X elements. There will be two cases, one for even and one for odd.
When N is even:
To make mean of N elements 0, best way is to express N elements as -X +X -X +X …. Formula will be sqrt(summation of (x^2)/n), x2+x^2+x^2+………N terms, so formula turns out to be sqrt (N*(x^2)/N). N cancel out each other, so sqrt (x^2) turns out to be SD. So, we get the N elements as -SD +SD -SD +SD…… to get the mean 0. We need to print -SD +SD -SD +SD……
When N is odd:
The mean of N elements will be 0. So, one element will be 0 and other N-1 elements will be -X +X -X …. Formula will be sqrt(summation of (x^2)/n), x2+x^2+x^2+………N-1 terms, so formula turns out to be sqrt((N-1)*(x^2)/N), so
X= SD * sqrt(n/(n-1)). The n elements are 0 -X +X -X +X …
When SD is 0 then all elements will be same, so we can print 0 for it.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void series( int n, int d)
{
if (d == 0) {
for ( int i = 0; i < n; i++)
cout << "0 " ;
cout << endl;
return ;
}
if (n % 2 == 0) {
for ( int i = 1; i <= n; i++) {
cout << pow (-1, i) * d << " " ;
}
cout << endl;
}
else
{
float m = n;
float r = (m / (m - 1));
float g = ( float )(d * ( float )sqrtf(r));
cout << "0 " ;
for ( int i = 1; i < n; i++) {
cout << pow (-1, i) * g << " " ;
}
cout << endl;
}
}
int main()
{
int n = 3, d = 3;
series(n, d);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
public class GfG {
public static void series( int n, int d)
{
if (d == 0 ) {
for ( int i = 0 ; i < n; i++)
System.out.print( "0 " );
System.out.println();
return ;
}
if (n % 2 == 0 ) {
for ( int i = 1 ; i <= n; i++) {
System.out.print(Math.pow(- 1 , i) * d + " " );
}
System.out.println();
}
else
{
float m = n;
float r = (m / (m - 1 ));
float g = ( float )(d * ( float )(Math.sqrt(r)));
System.out.print( "0 " );
for ( int i = 1 ; i < n; i++) {
System.out.print(Math.pow(- 1 , i) * g + " " );
}
System.out.println();
}
}
public static void main(String args[])
{
int n = 3 , d = 3 ;
series(n, d);
}
}
|
Python3
import math
def series( n, d):
if d = = 0 :
for i in range (n):
print ( "0" , end = ' ' )
return 1
if n % 2 = = 0 :
i = 1
while i < = n:
print ( "%.5f" % ((math. pow ( - 1 , i) * d)),
end = ' ' )
i + = 1
else :
m = n
r = (m / (m - 1 ))
g = ( float )(d * float (math.sqrt(r)))
print ( "0 " , end = ' ' )
i = 1
while i < n:
print ( "%.5f" % (math. pow ( - 1 , i) * g),
end = ' ' )
i = i + 1
print ( "\n" )
n = 3
d = 3
series(n, d)
|
C#
using System;
public class GfG {
public static void series( int n, int d)
{
if (d == 0) {
for ( int i = 0; i < n; i++)
Console.Write( "0" );
Console.WriteLine();
return ;
}
if (n % 2 == 0) {
for ( int i = 1; i <= n; i++) {
Console.Write(Math.Pow(-1, i)
* d + " " );
}
Console.WriteLine();
}
else
{
float m = n;
float r = (m / (m - 1));
float g = ( float )(d *
( float )(Math.Sqrt(r)));
Console.Write( "0 " );
for ( int i = 1; i < n; i++) {
Console.Write(Math.Pow(-1, i)
* g + " " );
}
Console.WriteLine();
}
}
public static void Main()
{
int n = 3, d = 3;
series(n, d);
}
}
|
PHP
<?php
function series( $n , $d )
{
if ( $d == 0)
{
for ( $i = 0; $i < $n ; $i ++)
echo "0 " ;
echo "\n" ;
return ;
}
if ( $n % 2 == 0)
{
for ( $i = 1; $i <= $n ; $i ++)
{
echo pow(-1, $i ) * $d , " " ;
}
echo "\n" ;
}
else
{
$m = $n ;
$r = ( $m / ( $m - 1));
$g = ( $d * sqrt( $r ));
echo "0 " ;
for ( $i = 1; $i < $n ; $i ++)
{
echo pow(-1, $i ) * $g , " " ;
}
echo "\n" ;
}
}
$n = 3; $d = 3;
series( $n , $d );
?>
|
Javascript
<script>
function series(n, d)
{
if (d == 0)
{
for (let i = 0; i < n; i++)
document.write( "0 " );
document.write();
return ;
}
if (n % 2 == 0)
{
for (let i = 1; i <= n; i++)
{
document.write(Math.pow(-1, i) * d + " " );
}
document.write();
}
else
{
let m = n;
let r = (m / (m - 1));
let g = (d * (Math.sqrt(r)));
document.write( "0 " );
for (let i = 1; i < n; i++)
{
document.write(Math.pow(-1, i) * g + " " );
}
document.write();
}
}
let n = 3, d = 3;
series(n, d);
</script>
|
Output:
0 -3.67423 3.67423
Time Complexity: O(N), as we are using a loop to traverse N times.
Auxiliary Space: O(1), as we are not using any extra space.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...