Problem Description
You are given a 0-indexed integer array nums and a positive integer k. You may perform the following operation any number of times: choose two distinct indices i and j and simultaneously update nums[i] to (nums[i] AND nums[j]) and nums[j] to (nums[i] OR nums[j]) (using bitwise AND/OR). After performing operations, you must choose exactly k elements from the final array and return the maximum possible sum of their squares modulo 10^9+7.
Key Insights
- The allowed operation does not change, for each bit position, the total number of ones across the array.
- For every bit b, if you let cnt[b] be the number of numbers that originally have the bth bit set, then cnt[b] remains invariant.
- Because each element is an integer constructed from bits, you can “transfer” bits between numbers. However, note that a given number can have at most one copy of a particular bit.
- To maximize the sum of squares when choosing k numbers, it is best to “concentrate” as many bits as possible into the same numbers (since (a+b)^2 > a^2 + b^2).
- For each bit b, if cnt[b] ≥ k then every selected number can receive that bit. Otherwise (cnt[b] < k), you have cnt[b] copies that must be placed into cnt[b] distinct numbers.
- An optimal final configuration for the k numbers is to let every number start with a “base” value (from all bits having at least k ones) and then, for the remaining bits (with cnt[b] < k), assign the bit to as many numbers as possible in a concentrated way. In fact, one optimal assignment is to set the i-th largest bucket’s extra sum as the sum of 2^b for every bit b for which cnt[b] ≥ i+1.
- The final answer is the sum over all k buckets of (base + extra_i)^2 modulo 10^9+7.
Space and Time Complexity
Time Complexity: O(n + k · B) where B is the number of bits (B ≤ 32)
Space Complexity: O(k) for storing the extra contributions per bucket
Solution
We start by counting for each bit position (from 0 to 31) the number of numbers in nums that have that bit set. For each bit:
- If cnt[b] ≥ k, we add 2^b to a variable called base. This means every chosen number will always have that bit.
- If cnt[b] < k (and at least one number has that bit), then we know there are cnt[b] extra copies that we must assign to cnt[b] distinct numbers. To maximize the sum of squares, we concentrate as many different bits as possible in the same bucket. An optimal assignment can be derived by “stacking” the extra bits: let extra[0] be increased by 2^b if cnt[b] ≥ 1, extra[1] increased if cnt[b] ≥ 2, and so on.
- Finally, for 0 ≤ i < k, the i-th chosen number is taken as base + extra[i]. The answer is then the sum of squares of these k numbers computed modulo 10^9+7.
The key “gotcha” is that although we can rearrange bits arbitrarily while obeying the constraint that a given bit may appear at most once in each number, if a bit appears fewer than k times overall it must be distributed among different numbers. This forces an optimal assignment where the i-th bucket receives contributions from those bits that occur at least i+1 times.
We now provide complete code solutions in multiple languages.