Sum the k array elements farthest from a target value

Quick Overview

Pick the k elements of an integer array that lie farthest from a target value and return their sum, breaking ties in distance by position. The problem tests top-k selection with a heap or a sort, building a custom ordering key, and handling equal distances so the result is unique.

Sum the k array elements farthest from a target value

Company: Sap

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given an integer array `nums`, an integer `target` and an integer `k`, choose the `k` elements of `nums` whose absolute difference from `target` is largest, and return the sum of those elements. ### Function Signature ```python def sum_of_k_farthest(nums: list[int], target: int, k: int) -> int: ``` ### Rules - The distance of an element `x` is `|x - target|`. Return the sum of the chosen elements themselves, not the sum of their distances. - Every position in `nums` is a separate element, so equal values at different positions can both be chosen. - When several elements have the same distance, the one with the smaller index is chosen first. Put another way: order the elements by distance from largest to smallest, keep their original order among equal distances, and sum the first `k`. ### Constraints - `1 <= nums.length <= 10^5` - `1 <= k <= nums.length` - `-10^4 <= nums[i] <= 10^4` - `-10^4 <= target <= 10^4` ### Examples ```text Input: nums = [1, 3, 5, 7, 9], target = 5, k = 2 Output: 10 ``` The distances from 5 are 4, 2, 0, 2, 4. The two largest belong to 1 and 9, so the sum is 1 + 9 = 10. ```text Input: nums = [2, -1, 10, 6], target = 3, k = 3 Output: 15 ``` The distances from 3 are 1, 4, 7, 3. The three farthest elements are 10, -1 and 6, and 10 + (-1) + 6 = 15. ```text Input: nums = [1, 3, 5, 7, 9], target = 5, k = 3 Output: 13 ``` After 1 and 9 (distance 4), the next distance is 2, shared by 3 at index 1 and 7 at index 3. The smaller index wins, so the chosen elements are 1, 9 and 3, and the sum is 13.

Overview: Pick the k elements of an integer array that lie farthest from a target value and return their sum, breaking ties in distance by position. The problem tests top-k selection with a heap or a sort, building a custom ordering key, and handling equal distances so the result is unique.

Sap logo
Sap
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given an integer array nums, an integer target and an integer k, choose the k elements of nums whose absolute difference from target is largest, and return the sum of those elements.

Function Signature

def sum_of_k_farthest(nums: list[int], target: int, k: int) -> int:

Rules

  • The distance of an element x is |x - target| . Return the sum of the chosen elements themselves, not the sum of their distances.
  • Every position in nums is a separate element, so equal values at different positions can both be chosen.
  • When several elements have the same distance, the one with the smaller index is chosen first. Put another way: order the elements by distance from largest to smallest, keep their original order among equal distances, and sum the first k .

Constraints

  • 1 <= nums.length <= 10^5
  • 1 <= k <= nums.length
  • -10^4 <= nums[i] <= 10^4
  • -10^4 <= target <= 10^4

Examples

Input: nums = [1, 3, 5, 7, 9], target = 5, k = 2
Output: 10

The distances from 5 are 4, 2, 0, 2, 4. The two largest belong to 1 and 9, so the sum is 1 + 9 = 10.

Input: nums = [2, -1, 10, 6], target = 3, k = 3
Output: 15

The distances from 3 are 1, 4, 7, 3. The three farthest elements are 10, -1 and 6, and 10 + (-1) + 6 = 15.

Input: nums = [1, 3, 5, 7, 9], target = 5, k = 3
Output: 13

After 1 and 9 (distance 4), the next distance is 2, shared by 3 at index 1 and 7 at index 3. The smaller index wins, so the chosen elements are 1, 9 and 3, and the sum is 13.