BackmediumArraysuncategorizedmedium

Optimize Traffic Volume Allocation Solution

Problem Statement

You are given an array of integers that represent traffic volumes recorded at different times. Your task is to redistribute the traffic so that every time slot receives the same volume. The volume assigned to each slot must be the arithmetic mean of all original volumes, rounded down to the nearest integer if necessary. The output should be an array of the same length where each element equals this computed mean.

Input format:

  • The first line contains a single integer n (1 ≤ n ≤ 10^5), the number of traffic records.
  • The second line contains n space‑separated integers nums[i] (−10^9 ≤ nums[i] ≤ 10^9), the original traffic volumes.

Output format:

  • Output a single line with n space‑separated integers, each equal to the floor of the average of the input array.

The solution must run in linear time and use only constant additional memory beyond the input and output arrays.

Example 1
Input
5 1 2 3 4 5
Output
3 3 3 3 3

Explanation: The sum of the array is 15. Dividing by 5 gives an average of 3.0, which is already an integer. Each of the five positions is replaced with 3.

Example 2
Input
4 10 20 30 40
Output
25 25 25 25

Explanation: Sum = 100. 100 / 4 = 25. Each element becomes 25.

Example 3
Input
3 -5 0 5
Output
0 0 0

Explanation: Sum = 0. 0 / 3 = 0. Each element becomes 0.

Constraints

  • 1 <= n <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • The sum of all nums[i] fits in a 64‑bit signed integer
  • The computed average is obtained by integer division (floor) of the sum by n
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Optimize Traffic Volume Allocation — Problem Statement & Solution Guide

ArraysMediumMixed
TimeO(n)
|
SpaceO(1)

Problem Description

You are given an array of integers that represent traffic volumes recorded at different times. Your task is to redistribute the traffic so that every time slot receives the same volume. The volume assigned to each slot must be the arithmetic mean of all original volumes, rounded down to the nearest integer if necessary. The output should be an array of the same length where each element equals this computed mean.

Input format:

- The first line contains a single integer n (1 ≤ n ≤ 10^5), the number of traffic records.

- The second line contains n space‑separated integers nums[i] (−10^9 ≤ nums[i] ≤ 10^9), the original traffic volumes.

Output format:

- Output a single line with n space‑separated integers, each equal to the floor of the average of the input array.

The solution must run in linear time and use only constant additional memory beyond the input and output arrays.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Optimize Traffic Volume Allocation"

medium

WHY DOES IT MATTER?

This pattern—compute a global aggregate once and reuse it—avoids redundant work and is a classic example of linear-time optimization. It teaches candidates to look for opportunities to replace nested loops with a single pass, a skill highly valued in performance‑critical code.

OPTIMIZATION CHALLENGE

The key insight is that the mean depends only on the total sum and the count, not on individual positions. By separating the sum calculation from the assignment, you eliminate O(n^2) behavior.

REAL-WORLD CONNECTION

Think of load balancing in a data center: you first measure total traffic, compute the desired per‑server load, and then redistribute traffic accordingly. The same one‑time measurement and broadcast pattern is used in many distributed systems.

When explaining this to an interviewer, emphasize that the algorithm is O(n) because you only traverse the array twice, and that the space overhead is minimal—just the output array.

COMPLEXITY AT A GLANCE

⏱ Time:O(n)
💾 Space:O(1)

Core Theory — Why This Approach?

The problem reduces to computing the arithmetic mean of an array of integers and then assigning that mean (rounded down) to every element. The arithmetic mean is defined as the sum of all elements divided by the number of elements. Because the output must be an integer, we take the floor of the exact mean, which is equivalent to integer division in most programming languages. A naive approach would recompute the sum for each position, leading to O(n^2) time. The optimal paradigm is a single linear scan to accumulate the total sum, followed by a constant‑time division to obtain the floor mean, and finally a second linear pass to populate the result array. This reduces time complexity to O(n) and space complexity to O(1) beyond the output array, making it suitable for very large inputs.

Interview Questions on This Problem

Q1How would you handle this problem if the input array could contain very large integers that might cause overflow when summing?

Use a 64‑bit integer type (e.g., long in Java, long long in C++) or a big integer library. Alternatively, compute the mean incrementally using a running average formula to avoid intermediate overflow: mean = mean + (x - mean)/i for each element i.

Q2In a distributed system, how would you compute the same uniform traffic allocation across multiple nodes without sending the entire array to a single coordinator?

Each node can compute a local sum and count, then participate in a reduce operation (e.g., MapReduce) to aggregate totals. Once the global mean is known, broadcast it back so each node can fill its local segment with the floor mean.

Q3What if the problem required you to preserve the relative order of the original array while redistributing traffic? Would that change the algorithm?

No, because the target value is the same for all positions; preserving order only matters if the output must be a permutation of the input, which is not the case here. The algorithm remains a single pass sum and a second pass fill.

Examples

Example 1

Input

5
1 2 3 4 5

Output

3 3 3 3 3

Explanation: The sum of the array is 15. Dividing by 5 gives an average of 3.0, which is already an integer. Each of the five positions is replaced with 3.

Example 2

Input

4
10 20 30 40

Output

25 25 25 25

Explanation: Sum = 100. 100 / 4 = 25. Each element becomes 25.

Example 3

Input

3
-5 0 5

Output

0 0 0

Explanation: Sum = 0. 0 / 3 = 0. Each element becomes 0.

Constraints

  • 1 <= n <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • The sum of all nums[i] fits in a 64‑bit signed integer
  • The computed average is obtained by integer division (floor) of the sum by n

Optimal Approach & Strategy

Compute the total sum once in O(n) time, divide by n to get the floor mean, and then fill the output array in another O(n) pass, achieving O(n) time and O(1) extra space.

Brute Force Approach

Recompute the sum of the entire array for each position, then divide by the length and floor, resulting in O(n^2) time.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function solution(nums) {
    let sum = nums.reduce((a, b) => a + b, 0);
    let average = sum / nums.length;
    return average;
}

Asked in Top Tech Interviews

uncategorizedmediumgeneric

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.