Skip to content
EasyArraysAI interview only

Left And Right Sum Differences

Asked atamazongooglebloomberg

01 · Problem

Given an integer array nums of length n, build an array answer of length n where:

  • leftSum[i] is the sum of the elements strictly to the left of index i (0 if there are none),
  • rightSum[i] is the sum of the elements strictly to the right of index i (0 if there are none),
  • answer[i] = |leftSum[i] - rightSum[i]|.

Return answer.

02 · Examples

Example 01
Input
nums = [10,4,8,3]
Output
[15,1,11,22]

leftSum = [0,10,14,22] and rightSum = [15,11,3,0], so the absolute differences are [15,1,11,22].

Example 02
Input
nums = [1]
Output
[0]

Both sums are 0 for the only element.

Example 03
Input
nums = [2,5,1]
Output
[6,1,7]

leftSum = [0,2,7] and rightSum = [6,1,0], giving [6,1,7].

03 · Constraints

  • 011 <= nums.length <= 1000
  • 021 <= nums[i] <= 105
  • 03answer.length == nums.length

04 · Optimal complexity

Time
O(n)
Space
O(1)
05 · Two ways to work on it

Practice it alone or rehearse it as an interview.

Practice Mode gives you an editor and test runs, nothing else. AI Interview Mode puts a voice interviewer on the other side, adds a clock, and ends with a scored summary of the round.