![]() |
VOOZH | about |
Given a positive integer N, the task is to find the average of the fourth powers of the first N natural numbers.
Examples:
Input: N = 3
Output: 32.6667
Explanation:
The sum of the fourth powers of the first N natural numbers = 14 + 24 + 34 = 1 + 16 + 81 = 98.
Therefore, the average = 98 / 3 = 32.6667.Input: N = 5
Output: 12
Naive Approach: The simplest approach to solve the given problem is to find the sum of the fourth powers of first N natural numbers and print its value when divided by N.
Below is the implementation of the above approach:
32.6667
Time Complexity: O(N)
Auxiliary Space: O(1)
Efficient Approach: The above approach can also be optimized by finding the sum of the fourth powers of the first N natural numbers by the mathematical formula given below and then print its value when divided by N.
The mathematical formula is as follows:
=>
=>
=>
Below is the implementation of the above approach:
32.6667
Time Complexity: O(1)
Auxiliary Space: O(1)