Skip to content

Practice Find if Array Can Be Sorted

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#3011
LeetCode Problem

Find if Array Can Be Sorted

Problem Statement

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, else return false.

Source: Find if Array Can Be Sorted on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nums = [8,4,2,30,15]
Output
true

# Example 2

Input
nums = [1,2,3,4,5]
Output
true

# Example 3

Input
nums = [3,16,8,4,2]
Output
false

Constraints

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

Related LeetCode Problems

Practice more problems similar to Find if Array Can Be Sorted.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.