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.
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.
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:
0 -3.67423 3.67423
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Mathematics | Mean, Variance and Standard Deviation
- Variance and standard-deviation of a matrix
- Program to implement standard deviation of grouped data
- Program for Mean Absolute Deviation
- Set in C++ Standard Template Library (STL)
- Standard Normal Distribution (SND) - Java Program
- Program to implement standard error of mean
- Calculate the IST : Indian Standard Time
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Count of Array elements greater than all elements on its left and next K elements on its right
- Find the XOR of the elements in the given range [L, R] with the value K for a given set of queries
- Find set of m-elements with difference of any two elements is divisible by k
- Find elements of array using XOR of consecutive elements
- Find smallest number with given number of digits and sum of digits under given constraints
- Generate an array of K elements such that sum of elements is N and the condition a[i] < a[i+1] <= 2*a[i] is met | Set 2
- Length of array pair formed where one contains all distinct elements and other all same elements
- Program to find sum of elements in a given array
- Find GCD of factorial of elements of given array
- Find the sum of elements of the Matrix generated by the given rules
- Find the deleted value from the array when average of original elements is given
Improved By : vt_m