BackeasyStringsGoogleAmazon

Protocol Tome Optimizer 28 Solution

Problem Statement

You are tasked with processing a sequence of integer values representing data points in a distributed system. Given an array of integers nums and a positive integer k, determine the sum of the k largest distinct values present in the array. If the array contains fewer than k distinct values, return the sum of all distinct values available. The solution must efficiently identify the top k elements without fully sorting the entire dataset if possible, leveraging traversal or selection techniques suitable for large-scale data processing.

The input consists of an array nums of length n and an integer k. The output is a single integer representing the computed sum. Note that 'largest' refers to the magnitude of the values, and duplicates should be considered only once in the selection of the top k distinct elements. For instance, if the array is [5, 5, 3, 1] and k=2, the distinct values are {5, 3, 1}, the two largest are 5 and 3, and the sum is 8.

Example 1
Input
nums = [10, 20, 30, 40, 50], k = 3
Output
120

Explanation: The distinct values are {10, 20, 30, 40, 50}. The 3 largest distinct values are 50, 40, and 30. Their sum is 50 + 40 + 30 = 120.

Example 2
Input
nums = [5, 5, 3, 1, 3], k = 2
Output
8

Explanation: The distinct values are {5, 3, 1}. The 2 largest distinct values are 5 and 3. Their sum is 5 + 3 = 8.

Example 3
Input
nums = [7, 7, 7], k = 5
Output
7

Explanation: The distinct values are {7}. Since there is only 1 distinct value and k=5, we sum all available distinct values. The sum is 7.

Example 4
Input
nums = [-1, -2, -3, -4], k = 2
Output
-3

Explanation: The distinct values are {-1, -2, -3, -4}. The 2 largest distinct values (closest to positive infinity) are -1 and -2. Their sum is -1 + (-2) = -3.

Constraints

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • 1 <= k <= 10^5
  • The sum of the k largest distinct elements will fit within a 64-bit integer.
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Protocol Tome Optimizer 28 — Problem Statement & Solution Guide

StringsEasyDFS Traversal
TimeO(n log k)
|
SpaceO(n)

Problem Description

You are tasked with processing a sequence of integer values representing data points in a distributed system. Given an array of integers nums and a positive integer k, determine the sum of the k largest distinct values present in the array. If the array contains fewer than k distinct values, return the sum of all distinct values available. The solution must efficiently identify the top k elements without fully sorting the entire dataset if possible, leveraging traversal or selection techniques suitable for large-scale data processing.

The input consists of an array nums of length n and an integer k. The output is a single integer representing the computed sum. Note that 'largest' refers to the magnitude of the values, and duplicates should be considered only once in the selection of the top k distinct elements. For instance, if the array is [5, 5, 3, 1] and k=2, the distinct values are {5, 3, 1}, the two largest are 5 and 3, and the sum is 8.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Protocol Tome Optimizer 28"

easy

WHY DOES IT MATTER?

Selecting top‑k distinct items appears in ranking, resource allocation, and alert prioritization.

OPTIMIZATION CHALLENGE

The key is to avoid full sorting and reduce work to O(n log k) by keeping only the needed candidates.

REAL-WORLD CONNECTION

Think of a monitoring system that only alerts on the k most severe unique error codes.

First deduplicate with a hash set, then stream values into a bounded min‑heap to stay memory‑efficient.

COMPLEXITY AT A GLANCE

⏱ Time:O(n log k)
💾 Space:O(n)

Core Theory — Why This Approach?

The naive solution collects all numbers, sorts them, and then iterates to pick the k largest distinct values. This approach incurs O(n log n) time and fails when n is huge because sorting the entire array is unnecessary work when only k elements are needed.

The optimal paradigm leverages a hash set to deduplicate values in O(n) time and then maintains a min‑heap of size at most k to track the top distinct elements. Each insertion into the heap costs O(log k), yielding an overall O(n log k) runtime while using O(n) auxiliary space for the set and O(k) for the heap.

Interview Questions on This Problem

Q1How would you handle the case when k exceeds the number of distinct elements?

After building the set of distinct values, compare its size with k. If the set is smaller, sum all elements in the set.

Q2Why is a min‑heap preferred over a max‑heap for this problem?

A min‑heap of size k lets us discard smaller values quickly when a larger distinct element appears. This keeps the heap bounded, guaranteeing O(log k) operations.

Q3Can you solve the problem in O(n) time without extra logarithmic factors?

Yes, by using the QuickSelect algorithm to find the k‑th largest distinct value after deduplication. Then a single pass sums all distinct values greater than or equal to that threshold.

Examples

Example 1

Input

nums = [10, 20, 30, 40, 50], k = 3

Output

120

Explanation: The distinct values are {10, 20, 30, 40, 50}. The 3 largest distinct values are 50, 40, and 30. Their sum is 50 + 40 + 30 = 120.

Example 2

Input

nums = [5, 5, 3, 1, 3], k = 2

Output

8

Explanation: The distinct values are {5, 3, 1}. The 2 largest distinct values are 5 and 3. Their sum is 5 + 3 = 8.

Example 3

Input

nums = [7, 7, 7], k = 5

Output

7

Explanation: The distinct values are {7}. Since there is only 1 distinct value and k=5, we sum all available distinct values. The sum is 7.

Example 4

Input

nums = [-1, -2, -3, -4], k = 2

Output

-3

Explanation: The distinct values are {-1, -2, -3, -4}. The 2 largest distinct values (closest to positive infinity) are -1 and -2. Their sum is -1 + (-2) = -3.

Constraints

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • 1 <= k <= 10^5
  • The sum of the k largest distinct elements will fit within a 64-bit integer.

Optimal Approach & Strategy

Use a hash set for deduplication and a min‑heap of size k to track the top distinct numbers in O(n log k) time.

Brute Force Approach

Sort the entire array, then walk it to collect distinct values until k are gathered, summing them.

Verified Code Solutions

JavaScript Solution
Time: O(n log k)
function solution(nums, k) {
   nums.sort((a, b) => b - a);
   let sum = 0;
   for (let i = 0; i < k; i++) {
       sum += nums[i];
   }
   return sum;
}

Asked in Top Tech Interviews

GoogleAmazonMicrosoft

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.