Tap here to switch tabs
Problem
Submissions

Problem: Maximum K to Sort a Permutation

med
30 min
Try to solve the Maximum K to Sort a Permutation problem.

Statement

You are given an integer array nums of length n, which is a permutation of the integers from 00 to n−1n - 1.

You can choose a non negative integer k. In one operation, you may swap two elements nums[i] and nums[j] if nums[i] & nums[j] == k. You can perform this operation any number of times.

Return the maximum value of k such that nums can be sorted in ascending order.

Constraints:

  • 1≤n==1 \leq n == nums.length ≤105\leq 10^5

  • 0≤0 \leq nums[i] ≤n−1\leq n - 1

  • nums is a permutation of integers from 00 to n−1n - 1.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Maximum K to Sort a Permutation

med
30 min
Try to solve the Maximum K to Sort a Permutation problem.

Statement

You are given an integer array nums of length n, which is a permutation of the integers from 00 to n−1n - 1.

You can choose a non negative integer k. In one operation, you may swap two elements nums[i] and nums[j] if nums[i] & nums[j] == k. You can perform this operation any number of times.

Return the maximum value of k such that nums can be sorted in ascending order.

Constraints:

  • 1≤n==1 \leq n == nums.length ≤105\leq 10^5

  • 0≤0 \leq nums[i] ≤n−1\leq n - 1

  • nums is a permutation of integers from 00 to n−1n - 1.