55. Left and Right Sum Differences

EasyArrayArray

Given an integer array nums of length n, return an integer array answer where answer[i] = |leftSum[i] - rightSum[i]|.

leftSum[i] = sum of all elements to the left of index i (or 0).
rightSum[i] = sum of all elements to the right of index i (or 0).

Input: An integer array nums of length n.

Output: Integer array answer of length n.

Examples

Example 1
Input: [10,4,8,3]
Output: [15,1,11,22]
Explanation: i=0: |0-(4+8+3)|=15. i=1: |10-(8+3)|=1. i=2: |(10+4)-3|=11. i=3: |(10+4+8)-0|=22.
Example 2
Input: [1]
Output: [0]
Explanation: Single element. leftSum=rightSum=0. |0-0|=0.
Example 3
Input: [1,2,3,4,5]
Output: [14,11,6,1,10]
Explanation: i=0:|0-14|=14. i=1:|1-12|=11. i=2:|3-9|=6. i=3:|6-5|=1. i=4:|10-0|=10.

Constraints

Asked by

GoogleBloombergMetaAmazon
Solve this problem in the editor →