Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Maximum K to Sort a Permutation

XPChallenge Points: 20
levelLevel: Medium

You are given an integer array perm of length n, where perm is a permutation of the numbers in the range [0, n - 1].
You may swap elements at indices i and j only if
perm[i] AND perm[j] == x,
where AND denotes the bitwise AND operation, and x is a non-negative integer.
Your task is to find the maximum value of x such that it is possible to sort the array perm in non-decreasing order using any number of such allowed swaps.
If the permutation is already sorted, return 0.

Example 1:

Input: perm = [2, 7, 6, 5, 3, 1, 0, 4]

Output: 0

Example 2:

Input: perm = [3, 0, 2, 1]

Output: 0

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result