GeeksforGeeks App
Open App
Browser
Continue

# Calculate Root Mean Kth power of all array elements

Given an array arr[] and an integer K, the task is to calculate the Kth root of the arithmetic mean of the Kth powers of all array elements.

Examples:

Input: arr[] = {1, 2, 3, 4, 5}, K = 2
Output: 3.31662
Explanation:
Sum of all Kth powers of array elements = 1 + 4 + 9 + 16 + 25 = 55
The value of root mean Kth power of array elements = √(55 / 5) = √11 = 3.31662

Input: arr[] = {10, 4, 6, 8}, K = 3
Output: 7.34847

Approach: The root mean of Kth powers of the array elements is given by the equation:

Therefore, the idea is to calculate the Kth power of each array element. Then, find the arithmetic mean of those elements. Finally, compute the Kth root of the calculated mean.

Below is the implementation of the above approach:

## C++

 // C++ program for the above approach #include using namespace std; // Function to find the Nth rootdouble nthRoot(int A, int N){    // Initially guessing random    // numberbetween 0 and 9    double xPre = rand() % 10;     // Smaller eps for more accuracy    double eps = 1e-3;     // Initialize difference between    // the two roots by INT_MAX    double delX = INT_MAX;     // xK denotes current value of x    double xK;     // Iterate until desired    // accuracy is reached    while (delX > eps) {         // Find the current value        // from previous value by        // newton's method        xK = ((N - 1.0) * xPre              + (double)A / pow(xPre, N - 1))             / (double)N;         delX = abs(xK - xPre);        xPre = xK;    }     return xK;} // Function to calculate the Root// Mean kth power of array elementsfloat RMNValue(int arr[], int n, int k){    int Nth = 0;     float mean = 0.0, root = 0.0;     // Calculate sum of kth power    for (int i = 0; i < n; i++) {        Nth += pow(arr[i], k);    }     // Calculate Mean    mean = (Nth / (float)(n));     // Calculate kth Root of mean    root = nthRoot(mean, k);     return root;} // Driver Codeint main(){    int arr[] = { 10, 4, 6, 8 };    int N = sizeof(arr) / sizeof(arr[0]);    int K = 3;     // Function Call    cout << RMNValue(arr, N, K);     return 0;}

## Java

 // Java program for// the above approachclass GFG{ // Function to find the  // Nth rootstatic double nthRoot(int A,                      int N){  // Initially guessing random  // numberbetween 0 and 9  double xPre = (Math.random() * 10) % 10;   // Smaller eps for more accuracy  double eps = 1e-3;   // Initialize difference between  // the two roots by Integer.MAX_VALUE  double delX = Integer.MAX_VALUE;   // xK denotes current value of x  double xK = 0;   // Iterate until desired  // accuracy is reached  while (delX > eps)  {    // Find the current value    // from previous value by    // newton's method    xK = ((N - 1.0) * xPre +          (double)A /          Math.pow(xPre, N - 1)) /          (double)N;     delX = Math.abs(xK - xPre);    xPre = xK;  }   return xK;} // Function to calculate the Root// Mean kth power of array elementsstatic float RMNValue(int arr[],                      int n, int k){  int Nth = 0;  float mean = 0, root = 0;   // Calculate sum of kth power  for (int i = 0; i < n; i++)  {    Nth += Math.pow(arr[i], k);  }   // Calculate Mean  mean = (Nth / (float)(n));   // Calculate kth Root of mean  root = (float) nthRoot((int)mean, k);   return root;} // Driver Codepublic static void main(String[] args){  int arr[] = {10, 4, 6, 8};  int N = arr.length;  int K = 3;   // Function Call  System.out.print(RMNValue(arr, N, K)); }} // This code is contributed by Rajput-Ji

## Python3

 # Python3 program for# the above approachimport sysimport random # Function to find# the Nth rootdef nthRoot(A, N):     # Initially guessing random    # numberbetween 0 and 9    xPre = random.random() % 10     # Smaller eps for    # more accuracy    eps = 1e-3     # Initialize difference between    # the two roots by INT_MAX    delX = sys.maxsize     # xK denotes current    # value of x    xK = 0     # Iterate until desired    # accuracy is reached    while (delX > eps):         # Find the current value        # from previous value by        # newton's method        xK = (((N - 1.0) * xPre +                A / pow(xPre, N - 1)) / N)        delX = abs(xK - xPre)        xPre = xK        return xK # Function to calculate the Root# Mean kth power of array elementsdef RMNValue(arr, n, k):     Nth = 0    mean = 0.0    root = 0.0     # Calculate sum of kth power    for i in range (n):        Nth += pow(arr[i], k)     # Calculate Mean    mean = (Nth // (n))     # Calculate kth Root of mean    root = nthRoot(mean, k)     return root # Driver Codeif __name__ == "__main__":     arr = [10, 4, 6, 8 ]    N = len(arr)    K = 3     # Function Call    print ( RMNValue(arr, N, K)) # This code is contributed by Chitranayal

## C#

 // C# program for the above approach using System; class GFG{  // Function to find the// Nth rootstatic double nthRoot(int A, int N){         // Instantiate random number generator    Random rand = new Random();             // Initially guessing random    // numberbetween 0 and 9    double xPre = (rand.Next() * 10) % 10;         // Smaller eps for more accuracy    double eps = 1e-3;         // Initialize difference between    // the two roots by Integer.MAX_VALUE    double delX = Int32.MaxValue;         // xK denotes current value of x    double xK = 0;         // Iterate until desired    // accuracy is reached    while (delX > eps)    {                 // Find the current value        // from previous value by        // newton's method        xK = ((N - 1.0) * xPre +              (double)A /              Math.Pow(xPre, N - 1)) /              (double)N;                 delX = Math.Abs(xK - xPre);        xPre = xK;    }    return xK;}  // Function to calculate the Root// Mean kth power of array elementsstatic float RMNValue(int[] arr, int n,                                 int k){    int Nth = 0;    float mean = 0, root = 0;         // Calculate sum of kth power    for(int i = 0; i < n; i++)    {        Nth += (int)Math.Pow(arr[i], k);    }         // Calculate Mean    mean = (Nth / (float)(n));         // Calculate kth Root of mean    root = (float)nthRoot((int)mean, k);         return root;}  // Driver Codepublic static void Main(){    int[] arr = { 10, 4, 6, 8 };    int N = arr.Length;    int K = 3;         // Function call    Console.Write(RMNValue(arr, N, K));}} // This code is contributed by code_hunt

## Javascript



Output:

7.65172

Time Complexity: O(N*log(sum)), where the sum is the sum of the square of all the given numbers in the array.
Auxiliary Space: O(1)

## Method:  Algorithmic approach

Steps:

• initialize a variable sum with zero
• Iterate through the array and calculate the Kth power of each element.
• Add the Kth power of each element to the sum.
• Calculate the arithmetic mean of Kth powers by dividing the sum by the length of the array.
• Take the Kth root of the arithmetic mean calculated in step 4 to get the final answer.

Below is the code implementation:

## C++

 #include #include using namespace std; int main() {      // Initialize the array and K    int arr[] = {1, 2, 3, 4, 5};    int K = 2;    int n = sizeof(arr)/sizeof(arr[0]);      // Initialize the sum to 0    double sum = 0.0;     for(int i=0; i

Output

3.31662

Time Complexity: O(n), where n is the number of elements in the input array.
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up