Related Articles

# Given N and Standard Deviation, find N elements

• Last Updated : 01 Oct, 2021

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.

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

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++

 `// CPP program to find n elements``#include ``using` `namespace` `std;` `// function to print series of n elements``void` `series(``int` `n, ``int` `d)``{` `    ``// if S.D. is 0 then print all``    ``// elements as 0.``    ``if` `(d == 0) {` `        ``// print n 0's``        ``for` `(``int` `i = 0; i < n; i++)``            ``cout << ``"0 "``;` `        ``cout << endl;``        ``return``;``    ``}` `    ``// if S.D. is even``    ``if` `(n % 2 == 0) {` `        ``// print -SD, +SD, -SD, +SD``        ``for` `(``int` `i = 1; i <= n; i++) {``            ``cout << ``pow``(-1, i) * d << ``" "``;``        ``}``        ``cout << endl;``    ``}``    ``else` `// if odd``    ``{``        ``// convert n to a float integer``        ``float` `m = n;``        ``float` `r = (m / (m - 1));``        ``float` `g = (``float``)(d * (``float``)sqrtf(r));` `        ``// print one element to be 0``        ``cout << ``"0 "``;` `        ``// print (n-1) elements as xi derived``        ``// from the formula``        ``for` `(``int` `i = 1; i < n; i++) {``            ``cout << ``pow``(-1, i) * g << ``" "``;``        ``}``        ``cout << endl;``    ``}``}` `// driver program to test the above function``int` `main()``{``    ``int` `n = 3, d = 3;``    ``series(n, d);` `    ``return` `0;``}`

## Java

 `// Java program to find n elements``import` `java.util.*;``import` `java.lang.*;` `public` `class` `GfG {` `    ``// function to print series of n elements``    ``public` `static` `void` `series(``int` `n, ``int` `d)``    ``{` `        ``// if S.D. is 0 then print all``        ``// elements as 0.``        ``if` `(d == ``0``) {` `            ``// print n 0's``            ``for` `(``int` `i = ``0``; i < n; i++)``                ``System.out.print(``"0 "``);``            ``System.out.println();``            ``return``;``        ``}` `        ``// if S.D. is even``        ``if` `(n % ``2` `== ``0``) {` `            ``// print -SD, +SD, -SD, +SD``            ``for` `(``int` `i = ``1``; i <= n; i++) {``                ``System.out.print(Math.pow(-``1``, i) * d + ``" "``);``            ``}``            ``System.out.println();``        ``}``        ``else` `// if odd``        ``{``            ``// convert n to a float integer``            ``float` `m = n;``            ``float` `r = (m / (m - ``1``));``            ``float` `g = (``float``)(d * (``float``)(Math.sqrt(r)));` `            ``// print one element to be 0``            ``System.out.print(``"0 "``);` `            ``// print (n-1) elements as xi``            ``// derived from the formula``            ``for` `(``int` `i = ``1``; i < n; i++) {``                ``System.out.print(Math.pow(-``1``, i) * g + ``" "``);``            ``}``            ``System.out.println();``        ``}``    ``}` `    ``// driver function``    ``public` `static` `void` `main(String args[])``    ``{``        ``int` `n = ``3``, d = ``3``;``        ``series(n, d);``    ``}``}` `/* This code is contributed by Sagar Shukla */`

## Python3

 `# Python program to find n elements``import` `math` `# function to print series of n elements``def` `series( n, d):` `    ``# if S.D. is 0 then print all``    ``# elements as 0.``    ``if` `d ``=``=` `0``:``    ` `        ``# print n 0's``        ``for` `i ``in` `range``(n):``            ``print``(``"0"``, end ``=` `' '``)``        ``return` `1``        ` `    ``# if S.D. is even``    ``if` `n ``%` `2` `=``=` `0``:``    ` `        ``# print -SD, +SD, -SD, +SD``        ``i ``=` `1``        ``while` `i <``=` `n:``            ``print``(``"%.5f"``%``((math.``pow``(``-``1``, i) ``*` `d)),``                  ``end ``=``' '``)``            ``i ``+``=` `1``    ``else``:``        ``# if odd``        ``# convert n to a float integer``        ``m ``=` `n``        ``r ``=` `(m ``/` `(m ``-` `1``))``        ``g ``=` `(``float``)(d ``*` `float``(math.sqrt(r)))``        ` `        ``# print one element to be 0``        ``print``(``"0 "``, end ``=` `' '``)``        ` `        ``# print (n-1) elements as xi derived``        ``# from the formula``        ``i ``=` `1``        ``while` `i < n:``            ``print``(``"%.5f"``%``(math.``pow``(``-``1``, i) ``*` `g),``                  ``end ``=` `' '``)``            ``i ``=` `i ``+` `1``    ``print``(``"\n"``)` `# driver code to test the above function``n ``=` `3``d ``=` `3``series(n, d)` `# This code is contributed by "Sharad_Bhardwaj".`

## C#

 `// C# program to find n elements``using` `System;` `public` `class` `GfG {` `    ``// function to print series of n``    ``// elements``    ``public` `static` `void` `series(``int` `n, ``int` `d)``    ``{` `        ``// if S.D. is 0 then print all``        ``// elements as 0.``        ``if` `(d == 0) {` `            ``// print n 0's``            ``for` `(``int` `i = 0; i < n; i++)``                ``Console.Write(``"0"``);``                ` `            ``Console.WriteLine();``            ` `            ``return``;``        ``}` `        ``// if S.D. is even``        ``if` `(n % 2 == 0) {` `            ``// print -SD, +SD, -SD, +SD``            ``for` `(``int` `i = 1; i <= n; i++) {``                ``Console.Write(Math.Pow(-1, i)``                                   ``* d + ``" "``);``            ``}``            ` `            ``Console.WriteLine();``        ``}``        ``else` `// if odd``        ``{``            ` `            ``// convert n to a float integer``            ``float` `m = n;``            ``float` `r = (m / (m - 1));``            ``float` `g = (``float``)(d *``                       ``(``float``)(Math.Sqrt(r)));` `            ``// print one element to be 0``            ``Console.Write(``"0 "``);` `            ``// print (n-1) elements as xi``            ``// derived from the formula``            ``for` `(``int` `i = 1; i < n; i++) {``                ``Console.Write(Math.Pow(-1, i)``                                   ``* g + ``" "``);``            ``}``            ` `            ``Console.WriteLine();``        ``}``    ``}` `    ``// driver function``    ``public` `static` `void` `Main()``    ``{``        ``int` `n = 3, d = 3;``        ` `        ``series(n, d);``    ``}``}` `// This code is contributed by vt_m.`

## PHP

 ``

## Javascript

 ``

Output:

`0 -3.67423 3.67423 `

My Personal Notes arrow_drop_up