BackmediumQueueGoogleAmazon

Pipeline Vector Partition 30 Solution

Problem Statement

Given an array of integers and an integer K, find the maximum sum of elements less than or equal to K that can be obtained without exceeding the total sum of the array.

Example 1
Input
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], K = 3
Output
18

Explanation: Step 1: Calculate the total sum of the array, which is 55. Step 2: Calculate K * nums.length, which is 30. Step 3: Since the total sum exceeds K * nums.length, we need to find the maximum sum of elements less than or equal to K that can be obtained without exceeding the total sum of the array. Step 4: Sort the array in ascending order. Step 5: Initialize a variable to store the maximum sum, which is 0. Step 6: Iterate over the sorted array and add each element to the maximum sum as long as it does not exceed K * nums.length. Step 7: Return the maximum sum, which is 18.

Example 2
Input
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100], K = 50
Output
500

Explanation: Step 1: Calculate the total sum of the array, which is 500. Step 2: Calculate K * nums.length, which is 500. Step 3: Since the total sum equals K * nums.length, we can return the total sum as the maximum sum. Step 4: Return the maximum sum, which is 500.

Constraints

  • 1 <= N <= 10^5
  • -10^4 <= metrics[i] <= 10^4
  • 1 <= K <= N
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 Partition 30 — Problem Statement & Solution Guide

QueueMediumRecursive Backtracking
TimeO(N·K)
|
SpaceO(K)

Problem Description

Given an array of integers and an integer K, find the maximum sum of elements less than or equal to K that can be obtained without exceeding the total sum of the array.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Pipeline Vector Partition 30"

medium

WHY DOES IT MATTER?

Subset‑sum appears in budgeting, resource allocation, and load‑balancing where a hard cap must not be exceeded.

OPTIMIZATION CHALLENGE

The key is to collapse the exponential search space into a linear scan over the capacity using DP or bitset tricks.

REAL-WORLD CONNECTION

Think of a shipping container that can hold at most K kilograms; you must pack items to maximize weight without overloading the container.

Always iterate sums backwards (or use a bitset) to avoid reusing the same element multiple times in one pass.

COMPLEXITY AT A GLANCE

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

Core Theory — Why This Approach?

The problem is a classic 0/1 subset‑sum: we must choose a subset of array elements whose total does not exceed K and is as large as possible. A naïve recursive enumeration explores 2^N subsets, which explodes for N>30, while dynamic programming reduces the state space to the capacity K, building a boolean reachable[] table that records which sums are achievable after processing each element, yielding a polynomial‑time solution. The optimal paradigm leverages DP or a bitset‑based convolution to update reachable sums in O(N·K) time and O(K) space, exploiting the fact that element values serve both as weight and profit, so the DP transition is simply reachable[s] = reachable[s] || reachable[s‑a[i]].

Interview Questions on This Problem

Q1How does the subset‑sum DP differ from the classic knapsack DP?

In subset‑sum the value equals the weight, so we only track feasibility of sums, not separate profit values. This simplifies the transition to a single boolean array.

Q2What optimization can reduce the DP’s memory footprint from O(N·K) to O(K)?

Iterate over the array and update the reachable sums in reverse order, reusing a single 1‑dimensional array. This eliminates the need for a second dimension for items.

Q3When would a bitset implementation be preferable to a boolean array?

When K is large (up to 10^5‑10^6) and the language provides fast word‑level operations, a bitset compresses 64 states per machine word and updates many sums in parallel. It cuts the constant factor dramatically.

Examples

Example 1

Input

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], K = 3

Output

18

Explanation: Step 1: Calculate the total sum of the array, which is 55. Step 2: Calculate K * nums.length, which is 30. Step 3: Since the total sum exceeds K * nums.length, we need to find the maximum sum of elements less than or equal to K that can be obtained without exceeding the total sum of the array. Step 4: Sort the array in ascending order. Step 5: Initialize a variable to store the maximum sum, which is 0. Step 6: Iterate over the sorted array and add each element to the maximum sum as long as it does not exceed K * nums.length. Step 7: Return the maximum sum, which is 18.

Example 2

Input

[10, 20, 30, 40, 50, 60, 70, 80, 90, 100], K = 50

Output

500

Explanation: Step 1: Calculate the total sum of the array, which is 500. Step 2: Calculate K * nums.length, which is 500. Step 3: Since the total sum equals K * nums.length, we can return the total sum as the maximum sum. Step 4: Return the maximum sum, which is 500.

Constraints

  • 1 <= N <= 10^5
  • -10^4 <= metrics[i] <= 10^4
  • 1 <= K <= N

Optimal Approach & Strategy

Use a DP/bitset to mark reachable sums while scanning the array, then pick the largest marked sum ≤ K.

Brute Force Approach

Enumerate every subset, compute its sum, and keep the best ≤ K; runs in O(2^N).

Verified Code Solutions

JavaScript Solution
Time: O(N·K)
function solution(nums, K) {
   let totalSum = nums.reduce((a, b) => a + b, 0);
   if (totalSum > K * nums.length) return 0;
   nums.sort((a, b) => a - b);
   let maxSum = 0;
   for (let i = 0; i < nums.length; i++) {
       if (nums[i] > K) break;
       maxSum += nums[i];
   }
   return maxSum;
}

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.