Calculate Root Mean Kth power of all array elements
Last Updated :
09 Oct, 2023
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++
#include <bits/stdc++.h>
using namespace std;
double nthRoot( int A, int N)
{
double xPre = rand () % 10;
double eps = 1e-3;
double delX = INT_MAX;
double xK;
while (delX > eps) {
xK = ((N - 1.0) * xPre
+ ( double )A / pow (xPre, N - 1))
/ ( double )N;
delX = abs (xK - xPre);
xPre = xK;
}
return xK;
}
float RMNValue( int arr[], int n, int k)
{
int Nth = 0;
float mean = 0.0, root = 0.0;
for ( int i = 0; i < n; i++) {
Nth += pow (arr[i], k);
}
mean = (Nth / ( float )(n));
root = nthRoot(mean, k);
return root;
}
int main()
{
int arr[] = { 10, 4, 6, 8 };
int N = sizeof (arr) / sizeof (arr[0]);
int K = 3;
cout << RMNValue(arr, N, K);
return 0;
}
|
Java
class GFG{
static double nthRoot( int A,
int N)
{
double xPre = (Math.random() * 10 ) % 10 ;
double eps = 1e- 3 ;
double delX = Integer.MAX_VALUE;
double xK = 0 ;
while (delX > eps)
{
xK = ((N - 1.0 ) * xPre +
( double )A /
Math.pow(xPre, N - 1 )) /
( double )N;
delX = Math.abs(xK - xPre);
xPre = xK;
}
return xK;
}
static float RMNValue( int arr[],
int n, int k)
{
int Nth = 0 ;
float mean = 0 , root = 0 ;
for ( int i = 0 ; i < n; i++)
{
Nth += Math.pow(arr[i], k);
}
mean = (Nth / ( float )(n));
root = ( float ) nthRoot(( int )mean, k);
return root;
}
public static void main(String[] args)
{
int arr[] = { 10 , 4 , 6 , 8 };
int N = arr.length;
int K = 3 ;
System.out.print(RMNValue(arr, N, K));
}
}
|
Python3
import sys
import random
def nthRoot(A, N):
xPre = random.random() % 10
eps = 1e - 3
delX = sys.maxsize
xK = 0
while (delX > eps):
xK = (((N - 1.0 ) * xPre +
A / pow (xPre, N - 1 )) / N)
delX = abs (xK - xPre)
xPre = xK
return xK
def RMNValue(arr, n, k):
Nth = 0
mean = 0.0
root = 0.0
for i in range (n):
Nth + = pow (arr[i], k)
mean = (Nth / / (n))
root = nthRoot(mean, k)
return root
if __name__ = = "__main__" :
arr = [ 10 , 4 , 6 , 8 ]
N = len (arr)
K = 3
print ( RMNValue(arr, N, K))
|
C#
using System;
class GFG{
static double nthRoot( int A, int N)
{
Random rand = new Random();
double xPre = (rand.Next() * 10) % 10;
double eps = 1e-3;
double delX = Int32.MaxValue;
double xK = 0;
while (delX > eps)
{
xK = ((N - 1.0) * xPre +
( double )A /
Math.Pow(xPre, N - 1)) /
( double )N;
delX = Math.Abs(xK - xPre);
xPre = xK;
}
return xK;
}
static float RMNValue( int [] arr, int n,
int k)
{
int Nth = 0;
float mean = 0, root = 0;
for ( int i = 0; i < n; i++)
{
Nth += ( int )Math.Pow(arr[i], k);
}
mean = (Nth / ( float )(n));
root = ( float )nthRoot(( int )mean, k);
return root;
}
public static void Main()
{
int [] arr = { 10, 4, 6, 8 };
int N = arr.Length;
int K = 3;
Console.Write(RMNValue(arr, N, K));
}
}
|
Javascript
<script>
function nthRoot(A, N)
{
var xPre = (Math.random() * 10) % 10;
var eps = 1e-3;
var delX = Number.MAX_VALUE;
var xK = 0;
while (delX > eps)
{
xK = ((N - 1.0) * xPre + A /
Math.pow(xPre, N - 1)) / N;
delX = Math.abs(xK - xPre);
xPre = xK;
}
return xK;
}
function RMNValue(arr, n, k)
{
var Nth = 0;
var mean = 0, root = 0;
for ( var i = 0; i < n; i++)
{
Nth += Math.pow(arr[i], k);
}
mean = (Nth / (n));
root = nthRoot(mean, k);
return root;
}
var arr = [ 10, 4, 6, 8 ];
var N = arr.length;
var K = 3;
document.write(RMNValue(arr, N, K));
</script>
|
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 <iostream>
#include <cmath>
using namespace std;
int main() {
int arr[] = {1, 2, 3, 4, 5};
int K = 2;
int n = sizeof (arr)/ sizeof (arr[0]);
double sum = 0.0;
for ( int i=0; i<n; i++) {
sum += pow (arr[i], K);
}
double mean = sum/n;
double result = pow (mean, 1.0/K);
cout << result << endl;
return 0;
}
|
Java
import java.util.Arrays;
public class MeanPowerRoot {
public static void main(String[] args) {
int [] arr = { 1 , 2 , 3 , 4 , 5 };
int K = 2 ;
int n = arr.length;
double sum = 0.0 ;
for ( int i = 0 ; i < n; i++) {
sum += Math.pow(arr[i], K);
}
double mean = sum / n;
double result = Math.pow(mean, 1.0 / K);
System.out.println(result);
}
}
|
Python3
import math
arr = [ 1 , 2 , 3 , 4 , 5 ]
K = 2
n = len (arr)
sum = 0.0
for i in range (n):
sum + = math. pow (arr[i], K)
mean = sum / n
result = math. pow (mean, 1.0 / K)
print (result)
|
C#
using System;
class Program
{
static void Main()
{
int [] arr = { 1, 2, 3, 4, 5 };
int K = 2;
int n = arr.Length;
double sum = 0.0;
for ( int i = 0; i < n; i++)
{
sum += Math.Pow(arr[i], K);
}
double mean = sum / n;
double result = Math.Pow(mean, 1.0 / K);
Console.WriteLine(result);
}
}
|
Javascript
const arr = [1, 2, 3, 4, 5];
const K = 2;
const n = arr.length;
let sum = 0.0;
for (let i = 0; i < n; i++) {
sum += Math.pow(arr[i], K);
}
const mean = sum / n;
const result = Math.pow(mean, 1.0 / K);
console.log(result);
|
Time Complexity: O(n), where n is the number of elements in the input array.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...