BackhardHeapZomatoApple

Bitmask Energy Vector Optimizer 2 Solution

Problem Statement

You are given an integer array nums of length N. While the array is non‑empty, repeatedly perform the following operation: locate the current minimum value m and the current maximum value M in the remaining elements. Compute the bitwise XOR of m and M and add the result to a running total. Then remove both m and M from the array. If only one element remains, add its value directly to the total and terminate. Return the final total after the array has been emptied. An efficient implementation should use a Min‑Max Priority Heap so that each extraction of the minimum and maximum runs in O(log N) time, yielding an overall O(N log N) solution.

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

Explanation: Initial array: [1,2,3,4] (sorted view). First extraction: min=1, max=4 → 1 XOR 4 = 5, total=5. Remove 1 and 4 → remaining [2,3]. Second extraction: min=2, max=3 → 2 XOR 3 = 1, total=5+1=6. Remove 2 and 3 → array empty. Final total = 6.

Example 2
Input
[7]
Output
7

Explanation: Only one element exists. According to the rule, its value is added directly to the total. Total = 7.

Example 3
Input
[5, 9, 1, 6, 2]
Output
17

Explanation: Step 1: min=1, max=9 → 1 XOR 9 = 8, total=8. Remove 1 and 9 → remaining [2,5,6]. Step 2: min=2, max=6 → 2 XOR 6 = 4, total=8+4=12. Remove 2 and 6 → remaining [5]. Step 3: single element 5 → add 5, total=12+5=17. Array empty, final total = 17.

Example 4
Input
[12, 15, 7, 3, 9, 20]
Output
30

Explanation: Step 1: min=3, max=20 → 3 XOR 20 = 23, total=23. Remove 3 and 20 → [7,9,12,15]. Step 2: min=7, max=15 → 7 XOR 15 = 8, total=23+8=31. Remove 7 and 15 → [9,12]. Step 3: min=9, max=12 → 9 XOR 12 = 5, total=31+5=36. Remove 9 and 12 → empty. Final total = 36.

Example 5
Input
[0, 0, 0, 0]
Output
0

Explanation: All elements are zero. Each XOR of min and max yields 0, and the sum remains 0 throughout. Final total = 0.

Constraints

  • 1 <= nums.length <= 2 * 10^5
  • -10^9 <= nums[i] <= 10^9
  • All operations must run in O(N log N) time or better
  • The solution must use only O(N) additional memory
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 Energy Vector Optimizer 2 — Problem Statement & Solution Guide

HeapHardMin-Max Priority Heap Queue
TimeO(N log N)
|
SpaceO(1) additional

Problem Description

You are given an integer array nums of length N. While the array is non‑empty, repeatedly perform the following operation: locate the current minimum value m and the current maximum value M in the remaining elements. Compute the bitwise XOR of m and M and add the result to a running total. Then remove both m and M from the array. If only one element remains, add its value directly to the total and terminate. Return the final total after the array has been emptied. An efficient implementation should use a Min‑Max Priority Heap so that each extraction of the minimum and maximum runs in O(log N) time, yielding an overall O(N log N) solution.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Bitmask Energy Vector Optimizer 2"

hard

WHY DOES IT MATTER?

Extract‑extremes‑repeatedly is a classic pattern that appears in load balancing, tournament scheduling, and greedy resource allocation. Mastering it teaches you how to turn an apparently dynamic problem into a static ordering problem, dramatically cutting runtime.

OPTIMIZATION CHALLENGE

The key insight is that after an initial global sort, the relative order of remaining elements never changes, so the minimum and maximum are always at the current ends of the sorted segment. This eliminates the need for repeated scans or heap rebalancing.

REAL-WORLD CONNECTION

Think of a server farm where the most under‑utilized node (minimum load) and the most overloaded node (maximum load) are paired to exchange tasks (XOR representing a transfer). By sorting loads once, you can schedule all exchanges in linear time, mirroring how batch job schedulers operate on sorted priority queues.

In an interview, write the sort‑then‑two‑pointer solution first; it’s concise, easy to verify, and avoids the boiler‑plate of two heaps. Mention the heap alternative only if the interviewer probes for different data‑structure trade‑offs.

COMPLEXITY AT A GLANCE

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

Core Theory — Why This Approach?

The operation repeatedly extracts the global minimum and maximum from a mutable multiset, computes their XOR, and accumulates the result. A naïve implementation would scan the entire array to find m and M at each step, leading to O(N^2) time for N removals, which is infeasible for N up to 2·10^5. The optimal paradigm leverages the fact that the ordering of elements never changes – only their availability does – so a single global sort (or a pair of priority queues) can provide the extremal values in O(1) after an O(N log N) preprocessing step. After sorting, the minimum and maximum are always at the two ends of the remaining segment, allowing a two‑pointer walk that removes both ends in O(1) per iteration. This reduces the overall complexity to O(N log N) time and O(1) extra space (aside from the input array).

Interview Questions on This Problem

Q1How would you compute the total XOR sum when repeatedly pairing the current minimum and maximum in an array of up to 10^5 elements?

Sort the array once (O(N log N)). Then use two indices, left = 0 and right = N‑1. While left < right, add nums[left] XOR nums[right] to the answer and move left++ and right--. If left == right after the loop, add nums[left] directly. This runs in O(N log N) time and O(1) extra space.

Q2Can you solve the same problem using two heaps? What are the trade‑offs compared to sorting?

Maintain a min‑heap for the smallest element and a max‑heap for the largest. At each step pop from both heaps, XOR them, and add to the total. When the heaps become empty or contain one element, handle the last element. This also yields O(N log N) time, but uses O(N) extra space and higher constant factors than the sort‑and‑two‑pointer method.

Q3Why does the XOR of min and max not depend on the order of removal, and can we prove that pairing extremes greedily yields the optimal total?

XOR is a bitwise, associative, and commutative operation, and the problem asks for the sum of XORs of disjoint pairs plus a possible singleton. Since each element participates exactly once, any pairing yields the same multiset of XOR terms; the greedy extreme pairing is simply a convenient way to generate a valid pairing without affecting the final sum. Formal proof follows from the fact that the operation is linear over GF(2) and the pairing is a partition of the set.

Examples

Example 1

Input

[3, 1, 4, 2]

Output

6

Explanation: Initial array: [1,2,3,4] (sorted view). First extraction: min=1, max=4 → 1 XOR 4 = 5, total=5. Remove 1 and 4 → remaining [2,3]. Second extraction: min=2, max=3 → 2 XOR 3 = 1, total=5+1=6. Remove 2 and 3 → array empty. Final total = 6.

Example 2

Input

[7]

Output

7

Explanation: Only one element exists. According to the rule, its value is added directly to the total. Total = 7.

Example 3

Input

[5, 9, 1, 6, 2]

Output

17

Explanation: Step 1: min=1, max=9 → 1 XOR 9 = 8, total=8. Remove 1 and 9 → remaining [2,5,6]. Step 2: min=2, max=6 → 2 XOR 6 = 4, total=8+4=12. Remove 2 and 6 → remaining [5]. Step 3: single element 5 → add 5, total=12+5=17. Array empty, final total = 17.

Example 4

Input

[12, 15, 7, 3, 9, 20]

Output

30

Explanation: Step 1: min=3, max=20 → 3 XOR 20 = 23, total=23. Remove 3 and 20 → [7,9,12,15]. Step 2: min=7, max=15 → 7 XOR 15 = 8, total=23+8=31. Remove 7 and 15 → [9,12]. Step 3: min=9, max=12 → 9 XOR 12 = 5, total=31+5=36. Remove 9 and 12 → empty. Final total = 36.

Example 5

Input

[0, 0, 0, 0]

Output

0

Explanation: All elements are zero. Each XOR of min and max yields 0, and the sum remains 0 throughout. Final total = 0.

Constraints

  • 1 <= nums.length <= 2 * 10^5
  • -10^9 <= nums[i] <= 10^9
  • All operations must run in O(N log N) time or better
  • The solution must use only O(N) additional memory

Optimal Approach & Strategy

Sort the array once and use two pointers to pair the smallest and largest remaining elements in O(N) after sorting, achieving O(N log N) total time.

Brute Force Approach

Repeatedly scan the whole array to find the current min and max, XOR them, add to total, and delete both elements; O(N^2) time.

Verified Code Solutions

JavaScript Solution
Time: O(N log N)
function solution(nums) {
   let minHeap = new MinHeap();
   let maxHeap = new MaxHeap();
   for (let num of nums) {
       minHeap.insert(num);
       maxHeap.insert(num);
   }
   let result = 0;
   while (!minHeap.isEmpty() || !maxHeap.isEmpty()) {
       let min = minHeap.extractMin();
       let max = maxHeap.extractMax();
       if (min === undefined) {
           min = 0;
       }
       if (max === undefined) {
           max = 0;
       }
       result += min + max;
   }
   return result;
}

Asked in Top Tech Interviews

ZomatoApple

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.