Given N and Standard Deviation, find N elements

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++
// CPP program to find n elements#include <bits/stdc++.h>using namespace std;// function to print series of n elementsvoid 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 functionint main(){ int n = 3, d = 3; series(n, d); return 0;} |
Java
// Java program to find n elementsimport 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 elementsimport math# function to print series of n elementsdef 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 functionn = 3d = 3series(n, d)# This code is contributed by "Sharad_Bhardwaj". |
C#
// C# program to find n elementsusing 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
<?php// PHP program to find n elements// function to print // series of n elementsfunction series( $n, $d){ // if S.D. is 0 then print all // elements as 0. if ($d == 0) { // print n 0's for ($i = 0; $i < $n;$i++) echo "0 "; echo "\n"; return; } // if S.D. is even if ($n % 2 == 0) { // print -SD, +SD, -SD, +SD for ( $i = 1; $i <= $n; $i++) { echo pow(-1, $i) * $d , " "; } echo"\n"; } // if odd else { // convert n to a // float integer $m = $n; $r = ($m / ($m - 1)); $g = ($d * sqrt($r)); // print one element // to be 0 echo "0 "; // print (n-1) elements // as xi derived // from the formula for ($i = 1; $i < $n; $i++) { echo pow(-1, $i) * $g , " "; } echo"\n"; }} // Driver Code $n = 3; $d = 3; series($n, $d);// This code is contributed by anuj_67.?> |
Javascript
<script>// JavaScript program to find n elements// Function to print series of n elementsfunction series(n, d){ // If S.D. is 0 then print all // elements as 0. if (d == 0) { // Print n 0's for(let i = 0; i < n; i++) document.write("0 "); document.write(); return; } // If S.D. is even if (n % 2 == 0) { // Print -SD, +SD, -SD, +SD for(let i = 1; i <= n; i++) { document.write(Math.pow(-1, i) * d + " "); } document.write(); } // If odd else { // Convert n to a float integer let m = n; let r = (m / (m - 1)); let g = (d * (Math.sqrt(r))); // Print one element to be 0 document.write("0 "); // Print (n-1) elements as xi // derived from the formula for(let i = 1; i < n; i++) { document.write(Math.pow(-1, i) * g + " "); } document.write(); }}// Driver Codelet n = 3, d = 3;series(n, d);// This code is contributed by susmitakundugoaldanga</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.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



