Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Solution: Sum of Absolute Differences in a Sorted Array
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Problem Statement

Given an integer array nums sorted in increasing order, return an array result of the same length, where result[i] should be the sum of the absolute differences between nums[i] and every other element in nums.

Examples

Example 1

  • Input: [1, 3, 6]
  • Output: [7, 5, 8]
  • Explanation:
    • For result[0]: |1-3| + |1-6| = 2 + 5 = 7
    • For result[1]: |3-1| + |3-6| = 2 + 3 = 5
    • For result[2]: |6-1| + |6-3| = 5 + 3 = 8

Example 2

  • Input: [2, 4, 7]
  • Output: [7, 5, 8]
  • Explanation:

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible