Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given an integer array nums of length n, where nums is a permutation 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,3,2,1]
Output: 1
Explanation:
Choose k = 1. Swapping nums[1] = 3 and nums[3] = 1 is allowed since nums[1] AND nums[3] == 1, resulting in a sorted permutation: [0, 1, 2, 3].
Example 2:
Input: nums = [0,1,3,2]
Output: 2
Explanation:
Choose k = 2. Swapping nums[2] = 3 and nums[3] = 2 is allowed since nums[2] AND nums[3] == 2, resulting in a sorted permutation: [0, 1, 2, 3].
Example 3:
Input: nums = [3,2,1,0]
Output: 0
Explanation:
Only k = 0 allows sorting since no greater k allows the required swaps where nums[i] AND nums[j] == k.
Constraints:
1 <= n == nums.length <= 1050 <= nums[i] <= n - 1nums is a permutation of integers from 0 to n - 1.Problem summary: You are given an integer array nums of length n, where nums is a permutation 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Bit Manipulation
[0,3,2,1]
[0,1,3,2]
[3,2,1,0]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3644: Maximum K to Sort a Permutation
class Solution {
public int sortPermutation(int[] nums) {
int ans = -1;
for (int i = 0; i < nums.length; ++i) {
if (i != nums[i]) {
ans &= nums[i];
}
}
return Math.max(ans, 0);
}
}
// Accepted solution for LeetCode #3644: Maximum K to Sort a Permutation
func sortPermutation(nums []int) int {
ans := -1
for i, x := range nums {
if i != x {
ans &= x
}
}
return max(ans, 0)
}
# Accepted solution for LeetCode #3644: Maximum K to Sort a Permutation
class Solution:
def sortPermutation(self, nums: List[int]) -> int:
ans = -1
for i, x in enumerate(nums):
if i != x:
ans &= x
return max(ans, 0)
// Accepted solution for LeetCode #3644: Maximum K to Sort a Permutation
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3644: Maximum K to Sort a Permutation
// class Solution {
// public int sortPermutation(int[] nums) {
// int ans = -1;
// for (int i = 0; i < nums.length; ++i) {
// if (i != nums[i]) {
// ans &= nums[i];
// }
// }
// return Math.max(ans, 0);
// }
// }
// Accepted solution for LeetCode #3644: Maximum K to Sort a Permutation
function sortPermutation(nums: number[]): number {
let ans = -1;
for (let i = 0; i < nums.length; ++i) {
if (i != nums[i]) {
ans &= nums[i];
}
}
return Math.max(ans, 0);
}
Use this to step through a reusable interview workflow for this problem.
Sort the array in O(n log n), then scan for the missing or unique element by comparing adjacent pairs. Sorting requires O(n) auxiliary space (or O(1) with in-place sort but O(n log n) time remains). The sort step dominates.
Bitwise operations (AND, OR, XOR, shifts) are O(1) per operation on fixed-width integers. A single pass through the input with bit operations gives O(n) time. The key insight: XOR of a number with itself is 0, which eliminates duplicates without extra space.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.