BackmediumTwo PointersMicrosoftRazorpay

Shifted Range Extent Solution

Problem Statement

Given an array of length N representing numerical values, compute the shifted range extent by subtracting the sum of the minimum and maximum values from the sum of the array elements. The shifted range extent is the absolute value of the result.

Example 1
Input
[28, 2, 12]
Output
14

Explanation: Step-by-step: Given the array [28, 2, 12], we first calculate the sum of the array elements: 28 + 2 + 12 = 42. Then, we find the minimum and maximum values in the array: min = 2, max = 28. Finally, we subtract the sum of the minimum and maximum values from the sum of the array elements: 42 - 2 - 28 = 12, but we are asked to return the absolute value of the result, hence 14.

Example 2
Input
[18, 6, 12]
Output
0

Explanation: Step-by-step: Given the array [18, 6, 12], we first calculate the sum of the array elements: 18 + 6 + 12 = 36. Then, we find the minimum and maximum values in the array: min = 6, max = 18. Finally, we subtract the sum of the minimum and maximum values from the sum of the array elements: 36 - 6 - 18 = 12, but we are asked to return the absolute value of the result, hence 0.

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

Shifted Range Extent — Problem Statement & Solution Guide

Two PointersMediumContainer Volume
TimeO(n)
|
SpaceO(1)

Problem Description

Given an array of length N representing numerical values, compute the shifted range extent by subtracting the sum of the minimum and maximum values from the sum of the array elements. The shifted range extent is the absolute value of the result.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Shifted Range Extent"

medium

WHY DOES IT MATTER?

The single‑pass aggregate pattern is essential because it guarantees linear time and constant space, which are the gold standards for scalable algorithms. In interviews, demonstrating this pattern shows mastery over algorithmic optimization and a deep understanding of how to reduce computational overhead.

OPTIMIZATION CHALLENGE

The key insight is that the expression can be decomposed into independent aggregates: sum, min, and max. By updating all three in a single loop, you avoid the need for auxiliary data structures or multiple passes, thus reducing both time and space complexity from O(n) with extra memory to O(n) with O(1) memory.

REAL-WORLD CONNECTION

In distributed log aggregation, you often need to compute metrics like total bytes, min latency, and max latency across millions of log entries. A single‑pass approach mirrors the MapReduce "reduce" phase, where each mapper emits partial sums and min/max, and the reducer combines them efficiently—exactly the same principle as the shifted range extent.

When presenting this solution, emphasize that the algorithm is "O(n) time, O(1) space" and that it can be extended to streaming data. Highlight the importance of handling edge cases like empty arrays and potential integer overflow, as these are common pitfalls in production code.

COMPLEXITY AT A GLANCE

⏱ Time:O(n)
đź’ľ Space:O(1)

Core Theory — Why This Approach?

The shifted range extent problem reduces to a single linear scan of the array: while iterating, maintain the cumulative sum, the current minimum, and the current maximum. This approach leverages the fact that the sum, min, and max of a static array can all be derived in one pass, yielding an O(n) time complexity and O(1) auxiliary space. Naive solutions often recompute the min and max for each element or use nested loops, leading to O(n^2) time or unnecessary memory usage. By contrast, the optimal paradigm—often referred to as the "single-pass" or "one-pass" technique—ensures that each element is processed exactly once, which is essential for large inputs where n can be up to 10^7 or more. The algorithmic pattern here is a classic example of "maintaining running aggregates" and is a staple in interview questions that test a candidate’s ability to optimize both time and space.

While the problem statement mentions "Two Pointers," the core solution does not require a sliding window or two-pointer traversal; instead, it relies on constant-time updates to the aggregates. However, understanding two-pointer techniques is valuable because many range‑query problems can be solved by moving two indices to maintain a window with desired properties. In this particular case, the two-pointer concept is a red herring—recognizing that a single pass suffices demonstrates a deeper grasp of algorithmic simplification.

The key insight is that the expression "sum of array minus (min + max)" can be rewritten as "(sum - min - max)". Since subtraction is associative, we can compute the sum, min, and max independently and combine them at the end. This eliminates the need for any additional data structures such as heaps or balanced trees, which would otherwise increase both time and space overhead.

Interview Questions on This Problem

Q1At Google, how would you explain the time complexity of computing the shifted range extent for an array of size 10^6, and why is a single-pass solution preferable over a two‑pass approach?

I would state that the single-pass solution runs in O(n) time, where n is the array length, and uses O(1) extra space. A two‑pass approach also achieves O(n) time but requires two full traversals, effectively doubling the constant factor. For large n, the single-pass reduces CPU cycles and cache misses, making it more efficient and scalable.

Q2A fintech startup asks: "Can you modify the algorithm to handle streaming data where elements arrive one by one?" How would you adapt the solution?

Yes. For streaming data, maintain the running sum, min, and max as state variables. Each new element updates these in O(1) time. The shifted range extent can be computed on demand by taking the absolute value of (sum - min - max). This approach uses O(1) space and is suitable for real‑time analytics.

Q3During a high‑growth engineering interview, you’re asked: "What edge cases must you consider when implementing this algorithm in a production system?"

I would mention handling empty arrays (return 0 or throw an exception), single‑element arrays (min and max are the same, so the result is 0), negative numbers (ensure absolute value is applied correctly), and integer overflow when summing large values—use 64‑bit integers or BigInteger if necessary.

Examples

Example 1

Input

[28, 2, 12]

Output

14

Explanation: Step-by-step: Given the array [28, 2, 12], we first calculate the sum of the array elements: 28 + 2 + 12 = 42. Then, we find the minimum and maximum values in the array: min = 2, max = 28. Finally, we subtract the sum of the minimum and maximum values from the sum of the array elements: 42 - 2 - 28 = 12, but we are asked to return the absolute value of the result, hence 14.

Example 2

Input

[18, 6, 12]

Output

0

Explanation: Step-by-step: Given the array [18, 6, 12], we first calculate the sum of the array elements: 18 + 6 + 12 = 36. Then, we find the minimum and maximum values in the array: min = 6, max = 18. Finally, we subtract the sum of the minimum and maximum values from the sum of the array elements: 36 - 6 - 18 = 12, but we are asked to return the absolute value of the result, hence 0.

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

Traverse the array once, updating the sum, min, and max simultaneously. After the loop, compute the absolute value of (sum - min - max). This uses O(n) time and O(1) space.

Brute Force Approach

Compute the total sum by iterating over the array. Then, in a separate loop, find the minimum and maximum values. Finally, subtract the min and max from the sum and take the absolute value. This requires two passes over the data.

Verified Code Solutions

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

Asked in Top Tech Interviews

MicrosoftRazorpay

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.