BackhardStringsAtlassianCred

Iterative Threshold Divergence Solution

Problem Statement

Given an array of numerical values, compute the iterative threshold divergence by calculating the sum of absolute differences between each element and the threshold, which is the average of all elements.

Example 1
Input
[1, 2, 3, 4, 5]
Output
20

Explanation: Step-by-step: Given the input array [1, 2, 3, 4, 5], we first calculate the threshold as the sum of all elements divided by the length of the array. Then, we iterate over the array, and for each element, we calculate the absolute difference between the element and the threshold. Finally, we return the sum of these differences, which is 20.

Example 2
Input
[10, 20, 30, 40, 50]
Output
28

Explanation: Step-by-step: Given the input array [10, 20, 30, 40, 50], we first calculate the threshold as the sum of all elements divided by the length of the array. Then, we iterate over the array, and for each element, we calculate the absolute difference between the element and the threshold. Finally, we return the sum of these differences, which is 28.

Constraints

  • 1 <= N <= 10^5
  • -10^9 <= arr[i] <= 10^9
  • Time Complexity expected: O(N) or O(N log N)
  • Space Complexity expected: O(1) or O(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

Iterative Threshold Divergence — Problem Statement & Solution Guide

StringsHardCharacter Frequency Map
TimeO(n)
|
SpaceO(1)

Problem Description

Given an array of numerical values, compute the iterative threshold divergence by calculating the sum of absolute differences between each element and the threshold, which is the average of all elements.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Iterative Threshold Divergence"

hard

WHY DOES IT MATTER?

The pattern exemplifies the "two‑pass aggregation" technique, where a global statistic is first derived and then used to compute a per‑element metric. Mastery of this pattern enables efficient solutions for many statistical and engineering problems, from signal processing to anomaly detection, where a central reference point guides further calculations.

OPTIMIZATION CHALLENGE

The breakthrough is recognizing that the mean is independent of element order and can be computed in O(n) without auxiliary structures. By separating the problem into sum‑then‑deviation phases, we eliminate the need for sorting or nested loops, collapsing a potentially quadratic process into linear time.

REAL-WORLD CONNECTION

In distributed monitoring systems, a central controller often computes the average latency across services and then measures each node's deviation to trigger alerts. The same two‑pass approach—first aggregate metrics, then evaluate deviations—ensures low overhead and real‑time responsiveness.

When coding under interview pressure, first write the code that computes the sum and count, verify the mean with a simple test case, then reuse the same loop (or a second concise loop) to accumulate absolute differences. Keep variable types consistent (use double for the mean) to avoid subtle bugs.

COMPLEXITY AT A GLANCE

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

Core Theory — Why This Approach?

The iterative threshold divergence problem asks for the total deviation of a numeric dataset from its central tendency, defined as the arithmetic mean. Mathematically, if S = Σ a[i] and n is the number of elements, the threshold T = S / n. The desired result is D = Σ |a[i] - T|. This formulation is a classic example of a linear‑time reduction: the mean can be obtained in a single pass, and once T is known, a second pass yields the absolute deviations. Naïve solutions that recompute the mean for each element or that sort the array to infer a median instead of the mean explode to O(n²) or O(n log n) time, which is unacceptable for large‑scale inputs common in interview settings. The optimal paradigm leverages the associative property of addition to aggregate the sum in O(n) time and then uses a simple arithmetic operation per element, preserving O(1) auxiliary space. This approach also highlights the importance of handling numeric precision—especially when dealing with integer division in languages that truncate—by using floating‑point arithmetic or rational representations.

In practice, the problem is a micro‑cosm of statistical preprocessing steps such as computing variance or mean absolute deviation, which are foundational in data‑driven systems. Recognizing that the mean is a global property that can be computed independently of element order allows us to avoid any sorting or complex data structures. The optimal solution therefore follows a two‑phase linear scan: first accumulate the total sum, compute the average, then accumulate the absolute differences. This pattern is a staple in interview coding because it tests a candidate’s ability to separate concerns, manage numeric types, and reason about algorithmic complexity.

The key theoretical insight is that absolute deviation is a convex function, and its aggregate can be expressed as a sum of independent contributions once the pivot (the mean) is fixed. Consequently, the problem reduces to two simple aggregations, each O(n), yielding an overall linear time algorithm with constant extra memory. Understanding this reduction is crucial for extending the technique to related metrics like variance (which requires a second moment) or weighted deviations.

Interview Questions on This Problem

Q1How would you compute the iterative threshold divergence for a stream of numbers where the total count is not known upfront?

Maintain two running aggregates: the count n and the sum S. For each incoming number x, increment n and add x to S. After processing the stream, compute the mean T = S / n, then either store the numbers to make a second pass for absolute differences or, if a single pass is required, use the online formula for mean absolute deviation which updates a running total of |x - T| using the previous mean and adjusts for the new mean.

Q2Why does using integer division to compute the threshold lead to incorrect divergence values, and how can you avoid it?

Integer division truncates the fractional part of the mean, causing the threshold to be off by up to 0.5 for each element, which accumulates into a noticeable error in the final sum. To avoid this, cast the sum or count to a floating‑point type before division, or use a rational representation (e.g., store numerator and denominator) and compute absolute differences using double precision.

Q3Can you extend the solution to compute the weighted iterative threshold divergence where each element has an associated weight?

Yes. Compute the weighted mean T = Σ (w[i] * a[i]) / Σ w[i] in a single pass by accumulating weighted sum and total weight. Then, in a second pass, sum the weighted absolute deviations Σ w[i] * |a[i] - T|. This still runs in O(n) time and O(1) extra space, assuming weights are provided alongside values.

Examples

Example 1

Input

[1, 2, 3, 4, 5]

Output

20

Explanation: Step-by-step: Given the input array [1, 2, 3, 4, 5], we first calculate the threshold as the sum of all elements divided by the length of the array. Then, we iterate over the array, and for each element, we calculate the absolute difference between the element and the threshold. Finally, we return the sum of these differences, which is 20.

Example 2

Input

[10, 20, 30, 40, 50]

Output

28

Explanation: Step-by-step: Given the input array [10, 20, 30, 40, 50], we first calculate the threshold as the sum of all elements divided by the length of the array. Then, we iterate over the array, and for each element, we calculate the absolute difference between the element and the threshold. Finally, we return the sum of these differences, which is 28.

Constraints

  • 1 <= N <= 10^5
  • -10^9 <= arr[i] <= 10^9
  • Time Complexity expected: O(N) or O(N log N)
  • Space Complexity expected: O(1) or O(N)

Optimal Approach & Strategy

Compute the total sum once to get the average, then in a second linear scan sum the absolute deviations, achieving O(n) time and O(1) extra space.

Brute Force Approach

Recompute the average for every element and sum the absolute differences, leading to O(n²) time.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function solution(nums) {
   if (nums.length === 0) return 0;
   let threshold = nums.reduce((a, b) => a + b, 0) / nums.length;
   return nums.reduce((a, b) => a + Math.abs(b - threshold), 0);
}

Asked in Top Tech Interviews

AtlassianCred

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.