Lexicographically Largest Power Array Problem
Description
LeetCode Problem 4059.
You are given an integer array nums of length n. You may rearrange its elements to form any permutation perm.
Define an array power of length 15. For each 0 <= i < 15, power[i] is the largest integer j, where 0 <= j <= n, such that the first j elements of perm all have the (14 - i)^th bit set.
Bit positions are indexed from right to left, starting with the 0^th bit.
Return the lexicographically largest possible power array.
Example 1:
Input: nums = [7,5]
Output: [0,0,0,0,0,0,0,0,0,0,0,0,2,1,2]
Explanation:
Choose perm = [7, 5].
- Both elements have bit 2 set, so power[12] = 2.
- The first element has bit 1 set, but the second does not, so power[13] = 1.
- Both elements have bit 0 set, so power[14] = 2.
All higher bits are unset in the first element, so the remaining entries are 0.
Example 2:
Input: nums = [3,1,7]
Output: [0,0,0,0,0,0,0,0,0,0,0,0,1,2,3]
Explanation:
Choose perm = [7, 3, 1].
- The first element has bit 2 set, but the second does not, so power[12] = 1.
- The first two elements have bit 1 set, but the third does not, so power[13] = 2.
- All three elements have bit 0 set, so power[14] = 3.
All higher bits are unset in the first element, so the remaining entries are 0.
Constraints:
1 <= nums.length <= 5 * 10^40 <= nums[i] < 2^15
Solution
Approach: Greedy over Bits with Superset Counts
Go through the bits from 14 down to 0, which is the order of power, and make each power[i] as large as possible given the earlier choices. This is correct because later entries can always be left as small as needed.
A set of choices power[i] = p_i is feasible when a permutation exists whose first p_i elements all contain bit 14 - i. The needed prefixes are nested, so Hall's condition only has to be checked at the distinct prefix lengths v: let mask_v be the bits whose chosen length is at least v. Then at least v numbers must contain every bit of mask_v.
Precompute sup[mask], the number of elements containing every bit of mask, with a superset-sum DP over the 2^15 masks. To choose the next value m, look at the segments between consecutive existing lengths. Inside one segment the mask is fixed (the old mask plus the new bit), so the best m there is min(segment end, sup[mask]). Stop as soon as an existing length v can no longer be satisfied with the new bit added, because every larger m would break it. Take the largest valid m.
Complexity: O(15 * 2^15 + 15^3) time and O(2^15) extra space.
class Solution:
def largestPower(self, nums: List[int]) -> List[int]:
n = len(nums)
sup = [0] * (1 << 15) # sup[mask] = how many numbers contain every bit of mask
for x in nums:
sup[x] += 1
for b in range(15):
for mask in range(1 << 15):
if not mask >> b & 1:
sup[mask] += sup[mask | 1 << b]
power = []
for b in range(14, -1, -1):
# levels: distinct positive prefix lengths chosen so far, with the bits that must hold up to them
levels = sorted(set(p for p in power if p > 0))
best = 0
prev = 0
for v in levels:
mask = 0
for i, p in enumerate(power):
if p >= v:
mask |= 1 << (14 - i)
c = sup[mask | 1 << b]
best = max(best, min(v, c) if min(v, c) > prev else best)
if c < v:
break
prev = v
else:
best = max(best, min(n, sup[1 << b]) if min(n, sup[1 << b]) > prev else best)
power.append(best)
return power