3644. Maximum K to Sort a Permutation

Mohd ShakeelMohd Shakeel
1 min read
class Solution {
    public int sortPermutation(int[] nums) {
        int n = nums.length;
        int max = 1;
        while(max < n){
            max<<=1;
        }
        max-=1;
        int k = max;
        for(int i=0; i<n; i++){
            if(nums[i]!=i){
                k&=nums[i];
            }
        }
        return(k==max)?0:k;
    }
}
0
Subscribe to my newsletter

Read articles from Mohd Shakeel directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Mohd Shakeel
Mohd Shakeel