BackhardLinked ListGoogleAmazon

Payload Token Evaluator 51 Solution

Problem Statement

Payload Token Evaluator 51

You are given a singly linked list of integers and an integer K. Your task is to compute the sum of the K largest values in the list that are strictly greater than K. If fewer than K such values exist, sum all of them. The list may contain negative numbers and duplicates.

Input format:

  • The first line contains an integer n, the number of nodes in the linked list.
  • The second line contains n space‑separated integers, the values of the nodes in order.
  • The third line contains the integer K.

Output format:

  • Output a single integer: the required sum.

The problem requires efficient handling of up to 10^5 nodes, so an O(n log K) or better algorithm is expected.

Example 1
Input
5 5 1 8 3 10 4
Output
23

Explanation: Values greater than 4 are 5, 8, and 10. Only three such values exist, which is less than K=4, so we sum all of them: 5+8+10=23.

Example 2
Input
5 2 -1 7 7 3 5
Output
14

Explanation: Values greater than 5 are 7 and 7. Only two values exist, less than K=5, so sum them: 7+7=14.

Example 3
Input
6 12 15 9 20 4 6 10
Output
47

Explanation: Values greater than 10 are 12, 15, and 20. Only three values exist, less than K=10, so sum them: 12+15+20=47.

Example 4
Input
5 1 2 3 4 5 3
Output
9

Explanation: Values greater than 3 are 4 and 5. Only two values exist, less than K=3, so sum them: 4+5=9.

Constraints

  • 1 <= n <= 100000
  • -1000000000 <= list[i] <= 1000000000
  • 1 <= K <= 100000
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

Payload Token Evaluator 51 — Problem Statement & Solution Guide

Linked ListHardGreedy Choice
TimeO(N log K)
|
SpaceO(K)

Problem Description

Payload Token Evaluator 51

You are given a singly linked list of integers and an integer K. Your task is to compute the sum of the K largest values in the list that are strictly greater than K. If fewer than K such values exist, sum all of them. The list may contain negative numbers and duplicates.

Input format:

- The first line contains an integer n, the number of nodes in the linked list.

- The second line contains n space‑separated integers, the values of the nodes in order.

- The third line contains the integer K.

Output format:

- Output a single integer: the required sum.

The problem requires efficient handling of up to 10^5 nodes, so an O(n log K) or better algorithm is expected.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Payload Token Evaluator 51"

hard

WHY DOES IT MATTER?

Selecting top‑K elements under a predicate is a common reduction pattern that avoids full sorting.

OPTIMIZATION CHALLENGE

The key is to shrink the problem from O(N log N) to O(N log K) by limiting stored state.

REAL-WORLD CONNECTION

It mirrors streaming analytics where only the highest‑value events above a threshold are retained.

Initialize the heap lazily and skip non‑qualifying nodes early to keep the loop tight.

COMPLEXITY AT A GLANCE

⏱ Time:O(N log K)
💾 Space:O(K)

Core Theory — Why This Approach?

The problem reduces to selecting the K largest elements that also satisfy the condition value > K, then summing them. A naive scan that sorts the entire list costs O(N log N) and is unnecessary because we only need the top K qualifying values, not a full ordering. The optimal paradigm leverages a min‑heap (or quick‑select) to maintain the K best candidates while traversing the list once, guaranteeing O(N log K) time and O(K) auxiliary space. This approach exploits the fact that maintaining a bounded priority queue avoids the overhead of sorting the whole dataset and scales gracefully for large N and small K, which is typical in interview constraints.

Interview Questions on This Problem

Q1Why is sorting the entire linked list suboptimal for this problem?

Sorting forces O(N log N) time even though we only need K elements. Maintaining a min‑heap of size K yields O(N log K), which is faster when K << N.

Q2How does a min‑heap help in extracting the K largest qualifying values in a single pass?

The heap keeps the smallest of the current top K, so any larger qualifying value can replace it. This ensures the heap always contains the K largest values seen so far.

Q3What edge case must you handle when the list contains fewer than K values greater than K?

You must sum all qualifying values instead of trying to access non‑existent elements. Detect this by checking the heap size after traversal.

Examples

Example 1

Input

5
5 1 8 3 10
4

Output

23

Explanation: Values greater than 4 are 5, 8, and 10. Only three such values exist, which is less than K=4, so we sum all of them: 5+8+10=23.

Example 2

Input

5
2 -1 7 7 3
5

Output

14

Explanation: Values greater than 5 are 7 and 7. Only two values exist, less than K=5, so sum them: 7+7=14.

Example 3

Input

6
12 15 9 20 4 6
10

Output

47

Explanation: Values greater than 10 are 12, 15, and 20. Only three values exist, less than K=10, so sum them: 12+15+20=47.

Example 4

Input

5
1 2 3 4 5
3

Output

9

Explanation: Values greater than 3 are 4 and 5. Only two values exist, less than K=3, so sum them: 4+5=9.

Constraints

  • 1 <= n <= 100000
  • -1000000000 <= list[i] <= 1000000000
  • 1 <= K <= 100000

Optimal Approach & Strategy

Traverse the list once, push qualifying values into a min‑heap of size at most K, popping the smallest when the heap exceeds K. Finally, sum the heap contents.

Brute Force Approach

Collect all values > K, sort them descending, then sum the first K (or all if fewer). This costs O(N log N) time and O(N) space.

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++) {
       if (nums[i] > k) {
           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.