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 = [6, 2, 7, 1]
Output: [10, 2, 7, 15]
Explanation: index 0: |0 - 10| = 10; index 1: |6 - 8| = 2; index 2: |8 - 1| = 7; index 3: |15 - 0| = 15.

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…