The Maximum AND Sum of Array problem gives you an integer array nums and an integer numSlots. There are numSlots slots, numbered from 1 to numSlots. You must place every element of the array into one of these slots. A single slot can hold at most two numbers. The score of placing a number in a slot is number AND slot_number (bitwise AND). Your goal is to return the maximum possible total score after placing all numbers.
This is an elite Hard-level DP problem. Interviewers from Google and Amazon ask it to assess a candidate's mastery of Bitmasking DP combined with Base-3 (Ternary) logic. Because each slot can hold up to 2 items, a standard binary bitmask (0 or 1) isn't enough. Candidates must prove they can model states dynamically using higher-base representations to avoid combinatorial explosion.
This problem strictly relies on Dynamic Programming with Base-3 State Masking.
We need to track the "capacity" of each slot. Since a slot can hold 0, 1, or 2 items, we can represent the state of all numSlots (max 9 slots) as a single integer in Base-3. A Base-3 number with 9 digits tops out at , which is a very small, highly cacheable state space!
We run a recursive DFS that takes the (current_num_index, base3_mask). Inside, we iterate through all slots from 1 to numSlots. If the slot's Base-3 digit in the mask is , we place the number there, add (num AND slot), update the mask, and recurse. Memoization caches the max score for each mask.
nums = [1, 2, 3], numSlots = 2.
Mask starts at 00 (Base 3: Slot 2 has 0, Slot 1 has 0).
Processing nums[0] = 1:
1 AND 1 = 1. Mask becomes 01. Recurse.
nums[1] = 2:
01 -> < 2, valid). Score 2 AND 1 = 0. Mask 02. Recurse.01 -> < 2, valid). Score 2 AND 2 = 2. Mask 11. Recurse.
By evaluating all branches using DFS and caching the results based on the Base-3 integer representation of the slots, the algorithm quickly converges on the optimal distribution layout.The most frequent mistake is trying to use a standard binary bitmask. Candidates will artificially duplicate the slots (e.g., Slot 1A, Slot 1B) to use a binary mask. While this works conceptually, it massively inflates the state space and often leads to Time Limit Exceeded errors due to redundant calculations. Understanding how to extract and update digits in Base-3 using / and % math is crucial for the optimal solution.
When dealing with state DP where items can have a limited count , always use an integer mask representing a higher base.
To get the -th digit of a mask in base : (mask / (B^i)) % B.
To add 1 to the -th digit of a mask in base : mask + (B^i).
Memorizing these two math operations allows you to implement Base-3, Base-4, or Base-K masks effortlessly.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Find the Minimum Cost Array Permutation | Hard | Solve | |
| Smallest Sufficient Team | Hard | Solve | |
| Concatenated Divisibility | Hard | Solve | |
| Minimum Time to Kill All Monsters | Hard | Solve | |
| Minimum XOR Sum of Two Arrays | Hard | Solve |