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
Inputnums = [8,4,2,30,15]
Outputtrue
By 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
Inputnums = [1,2,3,4,5]
Outputtrue
The array is already sorted, hence we return true.

Constraints

  • 1 <= nums.length <= 100
  • 1 <= nums[i] <= 2^8

Asked at 5 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate