Return Absolute Differences of Strictly Left and Right Sums

Quick Overview

Return each index’s absolute difference between the sums strictly to its left and right, treating empty sides as zero.

Return Absolute Differences of Strictly Left and Right Sums

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem For each index i in `nums`, sum entries strictly to its left and strictly to its right, using 0 for an empty side. Return the absolute difference of those two sums for every index in original order. ```cpp class Solution { public: vector<int> leftRightDifference(vector<int>& nums); }; ``` ### Examples **Example 1:** ```text Input: nums = [2,5,1] Output: [6,1,7] Explanation: Left sums are [0,2,7], and right sums are [6,1,0]. ``` **Example 2:** ```text Input: nums = [4,4] Output: [4,4] Explanation: Each index has one 4 on its opposite side and an empty other side. ``` ### Constraints - `1 <= nums.length <= 1000`. - `1 <= nums[i] <= 100000`. ### Clarifying Questions - Is the current element included in either sum? No. - What is an empty side's sum? Zero. - Is the difference signed? No; return its absolute value. - What order should answers use? The original index order. ```hint Check the boundary For a one-element array, both side sums are empty. ```

Overview: Return each index’s absolute difference between the sums strictly to its left and right, treating empty sides as zero.

Google logo
Google
Source window: 0–6 months · as of Oct 21, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

For each index i in nums, sum entries strictly to its left and strictly to its right, using 0 for an empty side. Return the absolute difference of those two sums for every index in original order.

class Solution {
public:
    vector<int> leftRightDifference(vector<int>& nums);
};

Examples

Example 1:

Input: nums = [2,5,1]
Output: [6,1,7]
Explanation: Left sums are [0,2,7], and right sums are [6,1,0].

Example 2:

Input: nums = [4,4]
Output: [4,4]
Explanation: Each index has one 4 on its opposite side and an empty other side.

Constraints

  • 1 <= nums.length <= 1000 .
  • 1 <= nums[i] <= 100000 .

Clarifying Questions Guidance

  • Is the current element included in either sum? No.
  • What is an empty side's sum? Zero.
  • Is the difference signed? No; return its absolute value.
  • What order should answers use? The original index order.