U

23. Sum and Integer Average of Array Elements

Easy

Problem Description

Calculate the total sum and integer average (truncated toward zero) of NN array integers.

Input Format

  • •Line 1: An integer NN (1≤N≤1051 \le N \le 10^5).
  • •Line 2: NN space-separated integers A1,A2,…,ANA_1, A_2, \dots, A_N.

Output Format

Print two space-separated integers: sum average.

Example 1:
Input:
4
10 20 30 40
Output:
100 25
Explanation: Sum = 100, Average = 100 / 4 = 25.
Example 2:
Input:
3
5 7 10
Output:
22 7
Explanation: Sum = 22, truncated average 22 / 3 = 7.
Example 3:
Input:
3
-10 -5 2
Output:
-13 -4
Explanation: Sum = -13, truncated average -13 / 3 = -4.
Constraints:
  • Standard integer bounds: 32-bit signed integer or input range [1, 10^5].
  • Time complexity target: O(N) or O(N log N) where applicable.
  • Auxiliary space target: O(1) or minimal auxiliary memory.
Follow up:Can this algorithm be adapted to an online streaming setting where the entire input cannot be held in memory simultaneously?
1,180 Online