#3644

Maximum K to Sort a Permutation

expert · 1065 · lc medium +32 · 37.2% accepted · 96 likes · top 15%

Description

nums is a permutation of [0..n-1]. You may swap nums[i] and nums[j] only when nums[i] & nums[j] == k for some non-negative integer k. Find the largest k for which any number of such swaps can sort the array in non-decreasing order. Return 0 if it is already sorted.

Code

1
2
3