Left and Right Sum Differences

Easy

Description

Given a 0-indexed integer array nums, find a 0-indexed integer array answer where answer[i] = |leftSum[i] - rightSum[i]|. leftSum is sum of elements to the left, rightSum to the right.

Examples

Input:nums = [10,4,8,3]
Output:[15,1,11,22]
Explanation:

Absolute differences.

Input:nums = [5,2,1,4]
Output:[7,0,3,8]
Explanation:

For i=0: |0 - 7| = 7. For i=1: |5 - 5| = 0. For i=2: |7 - 4| = 3. For i=3: |8 - 0| = 8. Result: [7,0,3,8].

Input:nums = [3,3,3]
Output:[6,0,6]
Explanation:

For each position i: leftSum[0]=0 (no elements to left), rightSum[0]=3+3=6 (elements to right), so ans[0]=|0-6|=6. leftSum[1]=3 (first element), rightSum[1]=3 (last element), so ans[1]=|3-3|=0. leftSum[2]=3+3=6 (first two elements), rightSum[2]=0 (no elements to right), so ans[2]=|6-0|=6. This shows how identical values create a symmetric pattern.

Constraints

  • 1 ≤ nums.length ≤ 1000

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!