Problem 343127 · easy · Level 03 Linear Management & Searching

Tilt at Every Position

prefix sums · suffix sums

For each index i of nums, the tilt is |leftSum - rightSum|, where leftSum is the sum of elements strictly before i and rightSum the sum of elements strictly after i (empty sides sum to 0). Return the list of tilts.

Examples

Input:  nums = [10, 4, 8, 3]
Output: [15, 1, 11, 22]
Explanation: index 0: |0 - 15| = 15; index 1: |10 - 11| = 1; index 2: |14 - 3| = 11; index 3: |22 - 0| = 22.

Input:  nums = [5]
Output: [0]

Constraints

  • 1 <= len(nums) <= 10**5
  • -10**5 <= nums[i] <= 10**5
  • Target complexity: O(n) time.

Goals

  • Compute left and right totals for every index in one pass
  • Avoid recomputing sums from scratch per index
Starting Python…