3644. Maximum K to Sort a Permutation
Difficulty: Medium
Topics: Staff, Array, Bit Manipulation, Weekly Contest 462
You are given an integer array nums of length n, where nums is a permutation1 of the numbers in the range [0..n - 1].
You may swap elements at indices i and j only if nums[i] AND nums[j] == k, where AND denotes the bitwise AND operation and k is a non-negative integer.
Return the maximum value of k such that the array can be sorted in non-decreasing order using any number of such swaps. If nums is already sorted, return 0.
Example 1:
- Input: nums = [0]
- Output: 0
-
Explanation: The array is already sorted, so no swaps are needed and the answer is
0.
Example 2:
- Input: nums = [0,3,2,1]
- Output: 1
-
Explanation: Choose
k = 1. Swappingnums[1] = 3andnums[3] = 1is allowed sincenums[1] AND nums[3] == 1, resulting in a sorted permutation:[0, 1, 2, 3].
Example 3:
- Input: nums = [0,1,3,2]
- Output: 2
-
Explanation: Choose
k = 2. Swappingnums[2] = 3andnums[3] = 2is allowed sincenums[2] AND nums[3] == 2, resulting in a sorted permutation:[0, 1, 2, 3].
Example 4:
- Input: nums = [3,2,1,0]
- Output: 0
-
Explanation: The misplaced values are
3, 2, 1, 0, and3 AND 2 AND 1 AND 0 == 0. Onlyk = 0allows the required swaps, so the answer is0.
Example 5:
- Input: nums = [1,0]
- Output: 0
-
Explanation: The only misplaced values are
1and0. Since1 AND 0 == 0, onlyk = 0allows the swap, so the answer is0.
Example 6:
- Input: nums = [0,5,2,3,4,1]
- Output: 1
-
Explanation: Choose
k = 1. Swappingnums[1] = 5andnums[5] = 1is allowed since5 AND 1 == 1, resulting in a sorted permutation:[0, 1, 2, 3, 4, 5].
Example 7:
- Input: nums = [0,1,2,6,4,5,3]
- Output: 2
-
Explanation: Choose
k = 2. Swappingnums[3] = 6andnums[6] = 3is allowed since6 AND 3 == 2, resulting in a sorted permutation:[0, 1, 2, 3, 4, 5, 6].
Example 8:
- Input: nums = [0,1,2,3,4,5,6,11,8,9,10,13,12,7]
- Output: 1
-
Explanation: Choose
k = 1. The misplaced values are11,13, and7. Using value1as a buffer, swaps such as11 AND 1 == 1,13 AND 1 == 1, and7 AND 1 == 1are allowed, so the permutation can be sorted. Since11 AND 13 AND 7 == 1, no largerkworks, so the answer is1.
Constraints:
1 <= n == nums.length <= 10⁵0 <= nums[i] <= n - 1-
numsis a permutation of integers from0ton - 1.
Hint:
- Take the bitwise AND of all elements that are not in their correct position.
Solution:
We compute the bitwise AND of every position/value that is not already in its correct place. For a permutation, the set of misplaced indices is the same as the set of misplaced values, so the provided PHP code can safely AND the indices i where nums[i] != i. If the array is already sorted, we return 0; otherwise, the maximum valid k is that AND mask.
Approach
- Traverse the permutation once.
- Track whether the array is already sorted.
- For every index
iwherenums[i] != i, includeiin the answer mask using bitwise AND. - If no mismatch is found, return
0. - Otherwise, return the accumulated AND mask.
Let's implement this solution in PHP: 3644. Maximum K to Sort a Permutation
<?php
/**
* @param Integer[] $nums
* @return Integer
*/
function sortPermutation(array $nums): int
{
...
...
...
/**
* go to ./solution.php
*/
}
// Test cases
echo sortPermutation([0]) . "\n"; // Output: 0
echo sortPermutation([0,3,2,1]) . "\n"; // Output: 1
echo sortPermutation([0,1,3,2]) . "\n"; // Output: 2
echo sortPermutation([3,2,1,0]) . "\n"; // Output: 0
echo sortPermutation([1,0]) . "\n"; // Output: 0
echo sortPermutation([0,5,2,3,4,1]) . "\n"; // Output: 1
echo sortPermutation([0,1,2,6,4,5,3]) . "\n"; // Output: 2
echo sortPermutation([0,1,2,3,4,5,6,11,8,9,10,13,12,7]) . "\n"; // Output: 1
?>
Explanation:
- A value not in its correct position must participate in at least one swap.
- If a swap between values
xandyis allowed for a givenk, thenx AND y == k, so every bit ofkmust also be present in bothxandy. - Therefore,
kmust be asubmaskof every misplaced value, meaningkcannot exceed the bitwise AND of all misplaced values. - Let
Mbe the bitwise AND of all misplaced values. ThenMis asubmaskof every misplaced value, soM AND x == Mfor every misplacedx. - The value
Malways exists in the permutation and can act as a connector/buffer for all misplaced values, sok = Mis always achievable. - Since no larger
kis possible, the maximum validkis exactlyM. - In a permutation, the set of misplaced indices equals the set of misplaced values, so the code can AND the indices
iwherenums[i] != i.
Complexity Analysis
-
Time Complexity:
O(n), because we scan the array once. -
Space Complexity:
O(1), because only a few scalar variables are used.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:
-
Permutation: A permutation is a rearrangement of all the elements of an array. ↩
Top comments (0)