BackeasyBinary SearchGoogleAmazon

Network Protocol Analyzer 50 Solution

Problem Statement

The task is to compute the total of all network metric values that exceed a given threshold. You are provided with a list of integer metrics and a single integer K. Your program must sum every metric that is strictly greater than K and output the resulting value.

Input format:

  • The first line contains an integer N, the number of metrics.
  • The second line contains N space‑separated integers representing the metrics.
  • The third line contains the integer K.

Output format:

  • A single integer: the sum of all metrics that are greater than K.

The solution should handle large inputs efficiently, using a linear scan of the array and 64‑bit arithmetic to avoid overflow.

Example 1
Input
5 1 3 5 7 9 4
Output
21

Explanation: Metrics greater than 4 are 5, 7, and 9. Their sum is 5 + 7 + 9 = 21.

Example 2
Input
6 -10 0 5 10 15 20 10
Output
35

Explanation: Metrics greater than 10 are 15 and 20. Their sum is 15 + 20 = 35.

Example 3
Input
4 100 200 300 400 500
Output
0

Explanation: No metric exceeds 500, so the sum is 0.

Constraints

  • 1 <= N <= 100000
  • -1000000000 <= metric[i] <= 1000000000
  • -1000000000 <= K <= 1000000000
  • The answer fits in a signed 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

Network Protocol Analyzer 50 — Problem Statement & Solution Guide

Binary SearchEasy2D Grid DP
TimeO(N log N) preprocessing + O(log N) per query
|
SpaceO(N) for the sorted array and prefix sums

Problem Description

The task is to compute the total of all network metric values that exceed a given threshold. You are provided with a list of integer metrics and a single integer K. Your program must sum every metric that is strictly greater than K and output the resulting value.

Input format:

- The first line contains an integer N, the number of metrics.

- The second line contains N space‑separated integers representing the metrics.

- The third line contains the integer K.

Output format:

- A single integer: the sum of all metrics that are greater than K.

The solution should handle large inputs efficiently, using a linear scan of the array and 64‑bit arithmetic to avoid overflow.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Network Protocol Analyzer 50"

easy

WHY DOES IT MATTER?

Efficient threshold queries are common in analytics and monitoring systems where latency matters.

OPTIMIZATION CHALLENGE

Transforming a linear aggregation into a logarithmic lookup plus constant‑time sum cuts runtime dramatically for large logs.

REAL-WORLD CONNECTION

Network dashboards often need to sum traffic volumes exceeding alert levels in real time.

Sort once, build a suffix‑sum array, and reuse it; avoid recomputing sums inside the binary‑search loop.

COMPLEXITY AT A GLANCE

⏱ Time:O(N log N) preprocessing + O(log N) per query
💾 Space:O(N) for the sorted array and prefix sums

Core Theory — Why This Approach?

Binary search exploits the monotonic property of sorted data to locate a target boundary in logarithmic time, turning linear scans into O(log N) lookups. When the problem requires aggregating values above a threshold, sorting the array once (O(N log N)) and then binary‑searching for the first element > K lets us compute the sum of the suffix in O(1) using a pre‑computed prefix‑sum array, yielding an overall O(N log N) solution.

A naïve linear scan that checks each element against K runs in O(N) time but still requires O(N) work for each query; if the threshold changes frequently or the dataset is huge, repeated scans become a bottleneck. The optimal paradigm combines sorting, binary search, and prefix sums to reduce per‑query work to constant time after an initial O(N log N) preprocessing step, which scales gracefully for large inputs and multiple queries.

Interview Questions on This Problem

Q1How does binary search achieve O(log N) time on a sorted array?

It repeatedly halves the search interval by comparing the middle element to the target, discarding half the remaining elements each step.

Q2Why might you prefer a prefix‑sum array after sorting for this problem?

A prefix‑sum lets you retrieve the sum of any suffix in O(1) time, avoiding repeated traversal of the tail segment.

Q3What is the overall complexity if you need to answer M different thresholds after preprocessing?

Preprocessing is O(N log N); each of the M queries is O(log N) for the binary search plus O(1) for the sum, so total O(N log N + M log N).

Examples

Example 1

Input

5
1 3 5 7 9
4

Output

21

Explanation: Metrics greater than 4 are 5, 7, and 9. Their sum is 5 + 7 + 9 = 21.

Example 2

Input

6
-10 0 5 10 15 20
10

Output

35

Explanation: Metrics greater than 10 are 15 and 20. Their sum is 15 + 20 = 35.

Example 3

Input

4
100 200 300 400
500

Output

0

Explanation: No metric exceeds 500, so the sum is 0.

Constraints

  • 1 <= N <= 100000
  • -1000000000 <= metric[i] <= 1000000000
  • -1000000000 <= K <= 1000000000
  • The answer fits in a signed 64‑bit integer.

Optimal Approach & Strategy

Sort the list, build a prefix‑sum array, binary‑search the first > K, then compute the suffix sum in O(1).

Brute Force Approach

Iterate through the list once, adding each value that is > K; this is O(N) for a single query.

Verified Code Solutions

JavaScript Solution
Time: O(N log N) preprocessing + O(log N) per query
const readline = require('readline');

const rl = readline.createInterface({
    input: process.stdin,
    terminal: false
});

let lines = [];
rl.on('line', line => {
    lines.push(line);
});

rl.on('close', () => {
    const n = parseInt(lines[0]);
    const metrics = lines[1].split(' ').map(Number);
    const k = parseInt(lines[2]);
    
    let sum = 0;
    for (let i = 0; i < n; i++) {
        if (metrics[i] > k) {
            sum += metrics[i];
        }
    }
    
    console.log(sum);
    
    process.exit(0);
});

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.