BackhardTwo PointersAmazonNetflix

Bitmask Subset Energy Calculator Solution

Problem Statement

You are given a sequence of N integers, where each integer represents the energy capacity of a vertical barrier placed at a distinct position along a straight line. When two barriers at positions i and j (i < j) are selected, the amount of energy that can be stored between them is defined as the product of the smaller of the two capacities and the distance between the barriers, i.e., min(nums[i], nums[j]) * (j - i). Your task is to determine the maximum possible stored energy that can be achieved by choosing any pair of barriers from the sequence.

Input is provided on two lines. The first line contains a single integer N, the number of barriers. The second line contains N space‑separated integers, the capacities of the barriers in order. The output should be a single integer, the maximum stored energy.

The problem requires an efficient solution that runs in linear time, as the input size can be large.

Example 1
Input
6 1 3 2 5 4 6
Output
12

Explanation: All possible pairs are examined: - (0,1): 1*1 = 1 - (0,2): 1*2 = 2 - (0,3): 1*3 = 3 - (0,4): 1*4 = 4 - (0,5): 1*5 = 5 - (1,2): 2*1 = 2 - (1,3): 3*2 = 6 - (1,4): 3*3 = 9 - (1,5): 3*4 = 12 - (2,3): 2*1 = 2 - (2,4): 2*2 = 4 - (2,5): 2*3 = 6 - (3,4): 4*1 = 4 - (3,5): 5*2 = 10 - (4,5): 4*1 = 4 The largest value is 12, obtained from the pair (1,5).

Example 2
Input
4 5 1 5 1
Output
10

Explanation: Pairs: - (0,1): 1*1 = 1 - (0,2): 5*2 = 10 - (0,3): 1*3 = 3 - (1,2): 1*1 = 1 - (1,3): 1*2 = 2 - (2,3): 1*1 = 1 The maximum is 10 from (0,2).

Example 3
Input
5 2 9 6 4 7
Output
21

Explanation: Pairs: - (0,1): 2*1 = 2 - (0,2): 2*2 = 4 - (0,3): 2*3 = 6 - (0,4): 2*4 = 8 - (1,2): 6*1 = 6 - (1,3): 4*2 = 8 - (1,4): 7*3 = 21 - (2,3): 4*1 = 4 - (2,4): 6*2 = 12 - (3,4): 4*1 = 4 The largest value is 21 from (1,4).

Constraints

  • 1 <= N <= 100000
  • 1 <= nums[i] <= 1000000000
  • The sum of N over all test cases does not exceed 200000
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

Bitmask Subset Energy Calculator — Problem Statement & Solution Guide

Two PointersHardContainer With Most Water
TimeO(N)
|
SpaceO(1)

Problem Description

You are given a sequence of N integers, where each integer represents the energy capacity of a vertical barrier placed at a distinct position along a straight line. When two barriers at positions i and j (i < j) are selected, the amount of energy that can be stored between them is defined as the product of the smaller of the two capacities and the distance between the barriers, i.e., min(nums[i], nums[j]) * (j - i). Your task is to determine the maximum possible stored energy that can be achieved by choosing any pair of barriers from the sequence.

Input is provided on two lines. The first line contains a single integer N, the number of barriers. The second line contains N space‑separated integers, the capacities of the barriers in order. The output should be a single integer, the maximum stored energy.

The problem requires an efficient solution that runs in linear time, as the input size can be large.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Bitmask Subset Energy Calculator"

hard

WHY DOES IT MATTER?

The two‑pointer pattern transforms quadratic pairwise comparisons into a linear scan, a critical optimization for any problem where a monotonic relationship exists between two moving indices. Mastery of this pattern unlocks efficient solutions for a wide class of array and string problems.

OPTIMIZATION CHALLENGE

The key insight is that the current area is bounded by the shorter barrier; therefore, any pair that keeps the shorter barrier fixed while reducing the distance cannot improve the result. This allows us to discard an entire set of pairs in O(1) time per iteration.

REAL-WORLD CONNECTION

Think of two engineers standing at opposite ends of a pipeline, measuring the maximum flow that can pass between them. As they move towards each other, the narrower pipe segment (the smaller height) dictates the flow, so they only need to adjust the position of the engineer at the bottleneck to find a better configuration.

During an interview, write the two‑pointer loop first, then immediately add the conditional move of the smaller height pointer. This keeps the code short, avoids off‑by‑one errors, and demonstrates that you understand the greedy elimination logic.

COMPLEXITY AT A GLANCE

⏱ Time:O(N)
💾 Space:O(1)

Core Theory — Why This Approach?

The problem asks for the maximum energy that can be stored between any two barriers, defined as min(nums[i], nums[j]) * (j - i). A naive double loop evaluates every pair, yielding O(N^2) time, which quickly becomes infeasible for N up to 10^5 or higher. The optimal solution leverages the monotonic nature of the min function: moving the pointer at the shorter barrier inward can only potentially increase the area because the distance shrinks while the height may increase. This insight enables a two‑pointer technique that scans the array from both ends, updating the best answer and discarding sub‑optimal pairs in linear time.

In each iteration, we compute the current energy using the two pointers, then compare the heights at those positions. The pointer pointing to the smaller height is moved inward because any pair involving that smaller height and a farther index cannot beat the current best (the distance would be smaller while the height would not increase). This greedy elimination guarantees that every possible maximal pair is examined exactly once, achieving O(N) time and O(1) auxiliary space.

Interview Questions on This Problem

Q1How does the two‑pointer approach guarantee that we don't miss the optimal pair in the Container With Most Water problem?

Because the area is limited by the shorter of the two heights, moving the taller pointer inward cannot increase the area for that shorter height; only moving the shorter pointer can potentially find a taller barrier that compensates for the reduced width. This greedy step ensures all viable candidates are examined.

Q2If the input array is sorted in non‑decreasing order, can we achieve a better than O(N) solution?

No. Even with a sorted array, the two‑pointer scan is already optimal at O(N). Sorting would add O(N log N) overhead, so the linear two‑pointer method remains the best.

Q3How would you modify the algorithm to also return the indices of the two barriers that achieve the maximum energy?

Maintain two variables, bestLeft and bestRight, updating them whenever a new maximum area is found during the two‑pointer traversal. At the end, return these indices along with the maximum energy.

Examples

Example 1

Input

6
1 3 2 5 4 6

Output

12

Explanation: All possible pairs are examined: - (0,1): 1*1 = 1 - (0,2): 1*2 = 2 - (0,3): 1*3 = 3 - (0,4): 1*4 = 4 - (0,5): 1*5 = 5 - (1,2): 2*1 = 2 - (1,3): 3*2 = 6 - (1,4): 3*3 = 9 - (1,5): 3*4 = 12 - (2,3): 2*1 = 2 - (2,4): 2*2 = 4 - (2,5): 2*3 = 6 - (3,4): 4*1 = 4 - (3,5): 5*2 = 10 - (4,5): 4*1 = 4 The largest value is 12, obtained from the pair (1,5).

Example 2

Input

4
5 1 5 1

Output

10

Explanation: Pairs: - (0,1): 1*1 = 1 - (0,2): 5*2 = 10 - (0,3): 1*3 = 3 - (1,2): 1*1 = 1 - (1,3): 1*2 = 2 - (2,3): 1*1 = 1 The maximum is 10 from (0,2).

Example 3

Input

5
2 9 6 4 7

Output

21

Explanation: Pairs: - (0,1): 2*1 = 2 - (0,2): 2*2 = 4 - (0,3): 2*3 = 6 - (0,4): 2*4 = 8 - (1,2): 6*1 = 6 - (1,3): 4*2 = 8 - (1,4): 7*3 = 21 - (2,3): 4*1 = 4 - (2,4): 6*2 = 12 - (3,4): 4*1 = 4 The largest value is 21 from (1,4).

Constraints

  • 1 <= N <= 100000
  • 1 <= nums[i] <= 1000000000
  • The sum of N over all test cases does not exceed 200000

Optimal Approach & Strategy

Use two pointers at the ends of the array, compute the current area, move the pointer at the smaller height inward, and update the maximum. This runs in O(N) time with O(1) extra space.

Brute Force Approach

Check every possible pair of indices (i, j) and compute min(nums[i], nums[j]) * (j - i), keeping the maximum. This requires O(N^2) time.

Verified Code Solutions

JavaScript Solution
Time: O(N)
function solution(nums) {
   let max_sum = nums[0];
   let current_sum = 0;
   for (let num of nums) {
       current_sum = Math.max(num, current_sum + num);
       max_sum = Math.max(max_sum, current_sum);
   }
   return max_sum;
}

Asked in Top Tech Interviews

AmazonNetflix

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.