Maximum AND Sum of Array - Problem
Maximum AND Sum of Array

You are given an integer array nums of length n and an integer numSlots such that 2 * numSlots >= n. There are numSlots slots numbered from 1 to numSlots.

Your task is to optimally place all n integers into the slots such that each slot contains at most two numbers. The AND sum of a given placement is the sum of the bitwise AND of every number with its respective slot number.

Example: Placing the numbers [1, 3] into slot 1 and [4, 6] into slot 2 gives:
(1 AND 1) + (3 AND 1) + (4 AND 2) + (6 AND 2) = 1 + 1 + 0 + 2 = 4

Return the maximum possible AND sum of nums given numSlots slots. This is a challenging optimization problem that requires exploring different assignment strategies to maximize the total bitwise AND sum.

Input & Output

example_1.py โ€” Basic case
$ Input: nums = [1,2,3,4,5,6], numSlots = 3
โ€บ Output: 9
๐Ÿ’ก Note: One optimal assignment is: Slot 1: [1,4] โ†’ (1&1)+(4&1) = 1+0 = 1, Slot 2: [2,6] โ†’ (2&2)+(6&2) = 2+2 = 4, Slot 3: [3,5] โ†’ (3&3)+(5&3) = 3+1 = 4. Total: 1+4+4 = 9
example_2.py โ€” Simple case
$ Input: nums = [1,3,10,4,7,1], numSlots = 9
โ€บ Output: 24
๐Ÿ’ก Note: One optimal assignment is to place each number in its own slot: (1&1)+(3&2)+(10&3)+(4&4)+(7&5)+(1&6) = 1+2+2+4+5+0 = 14. But we can do better by strategic placement to get 24.
example_3.py โ€” Edge case
$ Input: nums = [1], numSlots = 1
โ€บ Output: 1
๐Ÿ’ก Note: Only one number and one slot. The AND sum is simply (1&1) = 1.

Visualization

Tap to expand
Hotel Room Assignment ProblemRoom 1Guest: 3Rent: 3&1=1Room 2Guests: 4,6Rent: 4&2+6&2=6Room 3Guest: 7Rent: 7&3=3Bitmask State RepresentationRoom states encoded as: 00=empty, 01=one guest, 10=two guestsExample mask: 100001 = Room1(1 guest), Room2(2 guests), Room3(1 guest)DP: memo[number_index][room_states_mask] = maximum_rent_achievableTotal Maximum Rent: 1 + 6 + 3 = 10Time: O(n ร— 3^numSlots) | Space: O(3^numSlots)
Understanding the Visualization
1
Setup rooms
We have numSlots rooms numbered 1 to numSlots, each can hold up to 2 guests
2
Calculate rent
Each guest pays rent equal to (guest_number AND room_number)
3
Track occupancy
Use bitmask to efficiently track how many guests are in each room
4
Find optimal assignment
Use dynamic programming to try all valid assignments and maximize total rent
Key Takeaway
๐ŸŽฏ Key Insight: Use bitmask to efficiently represent all possible room occupancy states, and dynamic programming with memoization to avoid recomputing subproblems, making this exponential problem tractable.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n * 3^numSlots)

Each slot has 3 states, so 3^numSlots possible states, and we process n numbers

n
2n
โœ“ Linear Growth
Space Complexity
O(3^numSlots)

Memoization table stores results for all possible slot state combinations

n
2n
โšก Linearithmic Space

Constraints

  • n == nums.length
  • 1 โ‰ค numSlots โ‰ค 9
  • 1 โ‰ค n โ‰ค 2 * numSlots
  • 1 โ‰ค nums[i] โ‰ค 15
  • Each slot can contain at most 2 numbers
Asked in
Google 15 Microsoft 12 Amazon 8 Meta 6
23.8K Views
Medium Frequency
~35 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen