Sum and Average of an Array

You are given an integer array of size N. Your task is to calculate the sum and average of all elements in the array.

  • Sum: The total of all elements in the array.
  • Average: The sum of all elements divided by the number of elements.

Note : The average should be displayed with two digits after the decimal point.

Example 1:

Input:

nums = [10, 20, 30, 40, 50]

Output:

150
30.00

Explanation:

The sum of array elements is = 10 + 20 + 30 + 40 + 50 => 150.

The average of given array is = (sum of array elements) / (number of elements in array) => 150 / 5 => 30.00

Example 2:

Input:

nums = [-5, 0, -15, 10]

Output:

0
0.00

Explanation:

The sum of array elements is = -5 + 0 + (-15) + 10 => 0.

The average of given array is = (sum of array elements) / (number of elements in array) => 0 / 4 => 0.00

Now Your Turn!

Pick the correct output for the given input

Input:

nums = [10, 4, -6, 3]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= N <= 100
  • -1000 <= nums[i] <= 1000

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
int calculateSum(int arr[], int N) {
// Write your logic here
}
 
double calculateAverage(int arr[], int N) {
// Write your logic here
}
};
Test Case

Input:

Nums