Find if Array Can Be Sorted
You are given a 0-indexed array of positive integers nums.
In one operation, you can swap any two adjacent elements if they have the same number of set bits. You are allowed to do this operation any number of times, including zero.
Return true if you can sort the array in ascending order, otherwise return false.
Example 1
Input
nums = [8,4,2,30,15]Output
trueBy swapping adjacent numbers with the same number of set bits, the array can become
[2, 4, 8, 15, 30], so the answer is true.Example 2
Input
nums = [1,2,3,4,5]Output
trueThe array is already sorted, hence we return true.
Constraints
- 1 <= nums.length <= 100
- 1 <= nums[i] <= 2^8