BackmediumGraphsuncategorizedmedium

Constrained Magical Energy Sequence Solution

Problem Statement

You are given an array of integers representing the magical energy of essence vials and a target total magical energy. Find a sequence of essence vials such that the total magical energy equals the target and the total magical energy of any two adjacent vials does not exceed 942.

Example 1
Input
[1, 2, 3, 4, 5], 5
Output
[1, 4]

Explanation: Step-by-step: with input [1, 2, 3, 4, 5] and target 5, we can select essence vials with magical energy 1 and 4, since their total magical energy equals the target and the total magical energy of any two adjacent vials does not exceed 942.

Example 2
Input
[10, 20, 30, 40, 50], 60
Output
[10, 50] or [20, 40]

Explanation: Step-by-step: with input [10, 20, 30, 40, 50] and target 60, we can select essence vials with magical energy 10 and 50, or 20 and 40, since their total magical energy equals the target and the total magical energy of any two adjacent vials does not exceed 942.

Constraints

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

Constrained Magical Energy Sequence — Problem Statement & Solution Guide

GraphsMediumMixed
TimeO(n·target)
|
SpaceO(target)

Problem Description

You are given an array of integers representing the magical energy of essence vials and a target total magical energy. Find a sequence of essence vials such that the total magical energy equals the target and the total magical energy of any two adjacent vials does not exceed 942.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Constrained Magical Energy Sequence"

medium

WHY DOES IT MATTER?

This pattern blends subset‑sum with graph‑path feasibility, a common motif when resources have pairwise compatibility constraints. Mastering it equips engineers to tackle scheduling, load‑balancing, and combinatorial optimization problems where selections are not independent.

OPTIMIZATION CHALLENGE

The key insight is to collapse the exponential subset space into a linear sum dimension by remembering only the last element’s value. This reduces both time and space from exponential to O(n·target), making the algorithm tractable for moderate targets.

REAL-WORLD CONNECTION

Imagine allocating virtual machines (VMs) to a cluster where any two VMs placed on the same physical host must not exceed a power budget (942 W). The DP finds a set of VMs whose total CPU demand matches a target while ensuring any adjacent placement respects the power cap.

When coding, use a vector<unordered_set<int>> or a vector<bitset> for dp[sum] to store last values. Early exit as soon as target is reachable, and prune values larger than target to keep the state small.

COMPLEXITY AT A GLANCE

⏱ Time:O(n·target)
💾 Space:O(target)

Core Theory — Why This Approach?

The problem can be modeled as a constrained subset‑sum where the chosen elements must also form a feasible path in an implicit graph: each array element is a node and an edge exists between two nodes i and j if v[i] + v[j] ≤ 942. The goal is to find any walk whose node values sum to the target. A naïve solution would enumerate every subset and every permutation, leading to O(2^n·n!) time – impossible for n > 30. The optimal paradigm is dynamic programming with state compression. For each reachable total s we store the set of possible "last" values that can achieve s while respecting the adjacency rule. When processing a new element x we can extend any previously reachable sum s where the stored last value y satisfies y + x ≤ 942, creating a new reachable sum s + x with last value x. This DP runs in pseudo‑polynomial time O(n·target) because the sum dimension is bounded by the target, and the per‑sum state can be kept as a bitset or hash set of last values, yielding linear space in the target.

Interview Questions on This Problem

Q1How would you modify the classic subset‑sum DP to enforce a pairwise adjacency constraint like v[i] + v[j] ≤ 942?

Add an extra dimension to the DP that records the value of the last element used to reach a particular sum. The transition only allows adding a new element x if the previous last value y satisfies y + x ≤ 942. This turns the DP state into dp[sum] = set of possible last values.

Q2Explain why a greedy approach (e.g., always picking the smallest available vial) fails for this problem.

Greedy selection ignores the global sum requirement and the adjacency bound simultaneously. Picking the smallest values may leave a remainder that cannot be satisfied because the remaining large values would violate the adjacency limit, while a different ordering of larger values could succeed.

Q3In a large‑scale system, how could you parallelize the DP for this problem?

The DP can be split by sum ranges: each worker processes a slice of the sum dimension, maintaining its own map of last values. After processing an element, workers exchange frontier states for overlapping sums to propagate feasible transitions, similar to parallel prefix‑sum or map‑reduce over the sum axis.

Examples

Example 1

Input

[1, 2, 3, 4, 5], 5

Output

[1, 4]

Explanation: Step-by-step: with input [1, 2, 3, 4, 5] and target 5, we can select essence vials with magical energy 1 and 4, since their total magical energy equals the target and the total magical energy of any two adjacent vials does not exceed 942.

Example 2

Input

[10, 20, 30, 40, 50], 60

Output

[10, 50] or [20, 40]

Explanation: Step-by-step: with input [10, 20, 30, 40, 50] and target 60, we can select essence vials with magical energy 10 and 50, or 20 and 40, since their total magical energy equals the target and the total magical energy of any two adjacent vials does not exceed 942.

Constraints

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

Optimal Approach & Strategy

Use DP indexed by current sum and store the possible last vial values; extend states only when the adjacency condition holds, achieving pseudo‑polynomial time.

Brute Force Approach

Enumerate every subset of vials and, for each subset, try all permutations to check the adjacency rule and the total sum.

Verified Code Solutions

JavaScript Solution
Time: O(n·target)
function solution(nums, target) { 
       function backtrack(start, path, total) { 
           if (total === target) { 
               result.push([...path]); 
               return; 
           } 
           for (let i = start; i < nums.length; i++) { 
               if (total + nums[i] <= target) { 
                   if (path.length === 0 || total + nums[i] + path[path.length - 1] <= 942) { 
                       path.push(nums[i]); 
                       backtrack(i + 1, path, total + nums[i]); 
                       path.pop(); 
                   } 
               } 
           } 
       } 
       let result = []; 
       backtrack(0, [], 0); 
       return result; 
   }

Asked in Top Tech Interviews

uncategorizedmediumgeneric

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.