Trains the technique from
LeetCode 3644Maximum K to Sort a PermutationThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A rack holds n trays numbered 0 through n - 1, and nums is a permutation of those numbers giving the label on each tray.
A lifter is set to a whole number k. It may exchange the labels in two trays only when both of those labels carry every bit that k carries, that is when label & k == k for each of them. Exchanges may be made as often as wanted.
Return the largest k for which the lifter can bring the rack into increasing order, so that the label in every tray equals the tray's own number. At least one label starts out in the wrong tray.
Example 1
The labels 5 and 4 are the ones in the wrong trays. Both carry the third bit, and 5 also carries the lowest bit while 4 does not, so the bits they share come to 4.
Example 2
All four labels are in the wrong trays, and the label 0 carries no bits at all, so the bits shared by every misplaced label come to 0.
Example 3
Every label is in the wrong tray, and the label 0 is among them, so no bit is shared by all of them.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def largest_shared_mask(nums: list[int]) -> int:public int largestSharedMask(int[] nums)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.