BackmediumDynamic ProgrammingGoogleAmazon

Pipeline Vector Synthesizer 6 Solution

Problem Statement

In a high-throughput data processing architecture, a sequence of integer metrics is generated by a pipeline vector synthesizer. Each metric represents a scalar value derived from vector operations. The system requires an aggregation step to compute a specific target value based on a threshold parameter K.

Given an array of integers representing the pipeline metrics and an integer K, determine the sum of all elements in the array that are less than or equal to K. If no elements satisfy this condition, the result is zero. This computation is critical for calibrating the synthesizer's output stability.

Input: An array of integers metrics and an integer K. Output: An integer representing the sum of all metrics[i] such that metrics[i] <= K.

Example 1
Input
metrics = [12, 5, 8, 15, 3], K = 10
Output
16

Explanation: Iterate through the array: 12 > 10 (skip), 5 <= 10 (add 5), 8 <= 10 (add 8), 15 > 10 (skip), 3 <= 10 (add 3). Total sum = 5 + 8 + 3 = 16.

Example 2
Input
metrics = [1, 2, 3, 4, 5], K = 0
Output
0

Explanation: Iterate through the array: 1 > 0, 2 > 0, 3 > 0, 4 > 0, 5 > 0. No elements are less than or equal to 0. Total sum = 0.

Example 3
Input
metrics = [-5, -2, 0, 2, 5], K = 0
Output
-7

Explanation: Iterate through the array: -5 <= 0 (add -5), -2 <= 0 (add -2), 0 <= 0 (add 0), 2 > 0 (skip), 5 > 0 (skip). Total sum = -5 + (-2) + 0 = -7.

Example 4
Input
metrics = [100, 200, 300], K = 1000
Output
600

Explanation: Iterate through the array: 100 <= 1000 (add 100), 200 <= 1000 (add 200), 300 <= 1000 (add 300). Total sum = 100 + 200 + 300 = 600.

Constraints

  • 1 <= metrics.length <= 10^5
  • -10^9 <= metrics[i] <= 10^9
  • -10^9 <= K <= 10^9
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

Pipeline Vector Synthesizer 6 — Problem Statement & Solution Guide

Dynamic ProgrammingMedium2D Grid DP
TimeO(N·K)
|
SpaceO(K)

Problem Description

In a high-throughput data processing architecture, a sequence of integer metrics is generated by a pipeline vector synthesizer. Each metric represents a scalar value derived from vector operations. The system requires an aggregation step to compute a specific target value based on a threshold parameter K.

Given an array of integers representing the pipeline metrics and an integer K, determine the sum of all elements in the array that are less than or equal to K. If no elements satisfy this condition, the result is zero. This computation is critical for calibrating the synthesizer's output stability.

Input: An array of integers metrics and an integer K.

Output: An integer representing the sum of all metrics[i] such that metrics[i] <= K.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Pipeline Vector Synthesizer 6"

medium

WHY DOES IT MATTER?

Subset‑sum DP is a cornerstone of combinatorial optimization; it teaches how to compress exponential search spaces into linear‑size tables by exploiting overlapping sub‑problems and optimal substructure.

OPTIMIZATION CHALLENGE

The key insight is to iterate the DP array backwards when processing each element. This guarantees that each element is counted at most once per sum, turning a potential O(N·K·N) update into a tight O(N·K) solution.

REAL-WORLD CONNECTION

Think of a cloud‑based budgeting service that must decide which expense items (array elements) to approve so that the total approved spend hits a regulatory cap K. The DP mirrors the service's decision engine, efficiently enumerating all feasible approval combinations.

When coding, always use a 1‑D DP vector of size K+1 and update it in reverse order. This pattern avoids the need for a second temporary array and dramatically reduces both runtime and memory footprints.

COMPLEXITY AT A GLANCE

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

Core Theory — Why This Approach?

The problem is a classic variant of the subset‑sum/count‑ways DP. For each prefix of the array we maintain how many ways we can achieve every possible sum up to the target K. The recurrence is simple: when we process a new element a[i], every previously reachable sum s can either stay unchanged (skip a[i]) or be extended to s + a[i] (take a[i]), provided s + a[i] ≤ K. By iterating the array once and updating a one‑dimensional DP table from high to low indices we avoid double‑counting. A naïve recursive solution explores 2^N subsets, which quickly explodes for N > 30, making it infeasible for the typical constraints (N up to 10^5, K up to 10^4). The DP formulation collapses the exponential state space into O(N·K) time and O(K) space, which is optimal for this class of problems because each element can affect every sum up to K exactly once.

Interview Questions on This Problem

Q1How would you modify the DP if the problem asked for the minimum number of elements needed to reach sum K instead of the count of ways?

Replace the count DP with a min‑count DP where dp[s] stores the smallest number of elements required to achieve sum s. Initialize dp[0]=0 and dp[others]=∞. For each a[i], update dp[s] = min(dp[s], dp[s‑a[i]]+1) for s from K down to a[i]. The answer is dp[K] if it is finite, otherwise -1.

Q2Explain how you could solve the same problem when the array contains negative numbers.

Negative numbers break the monotonicity of the sum range, so a simple 0…K DP no longer works. One approach is to shift all possible sums by adding an offset equal to the absolute sum of all negative numbers, turning the range into a non‑negative interval, and then apply the same DP over the enlarged range. Alternatively, use a hashmap to store only reachable sums, updating it iteratively; this runs in O(N·M) where M is the number of distinct reachable sums.

Q3What is the time‑space trade‑off if you need to answer multiple queries of different K on the same array?

You can pre‑compute a DP table dp[i][s] = number of ways to reach sum s using the first i elements, which costs O(N·Smax) time and space, where Smax is the maximum K across queries. Then each query is answered in O(1) by reading dp[N][K]. If space is a concern, keep only the final 1‑D dp array and recompute for each distinct K, yielding O(N·K) per query but O(K) space.

Examples

Example 1

Input

metrics = [12, 5, 8, 15, 3], K = 10

Output

16

Explanation: Iterate through the array: 12 > 10 (skip), 5 <= 10 (add 5), 8 <= 10 (add 8), 15 > 10 (skip), 3 <= 10 (add 3). Total sum = 5 + 8 + 3 = 16.

Example 2

Input

metrics = [1, 2, 3, 4, 5], K = 0

Output

0

Explanation: Iterate through the array: 1 > 0, 2 > 0, 3 > 0, 4 > 0, 5 > 0. No elements are less than or equal to 0. Total sum = 0.

Example 3

Input

metrics = [-5, -2, 0, 2, 5], K = 0

Output

-7

Explanation: Iterate through the array: -5 <= 0 (add -5), -2 <= 0 (add -2), 0 <= 0 (add 0), 2 > 0 (skip), 5 > 0 (skip). Total sum = -5 + (-2) + 0 = -7.

Example 4

Input

metrics = [100, 200, 300], K = 1000

Output

600

Explanation: Iterate through the array: 100 <= 1000 (add 100), 200 <= 1000 (add 200), 300 <= 1000 (add 300). Total sum = 100 + 200 + 300 = 600.

Constraints

  • 1 <= metrics.length <= 10^5
  • -10^9 <= metrics[i] <= 10^9
  • -10^9 <= K <= 10^9

Optimal Approach & Strategy

Use a one‑dimensional DP that records the number of ways to achieve each sum up to K, updating it in reverse for each array element.

Brute Force Approach

Enumerate every subset of the array (2^N possibilities) and count those whose sum equals K.

Verified Code Solutions

JavaScript Solution
Time: O(N·K)
function solution(nums, K) {
   let sum = 0;
   for (let num of nums) {
       if (num <= K) {
           sum += num;
       }
   }
   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.