Skip to main content
Back to problems
Leetcode
Medium
Arrays
Bit Manipulation
Sorting
Find If Array Can Be Sorted

Determine whether an array can be transformed into nondecreasing order under a bit-based swap restriction.

Acceptance 0%
Problem Statement

Problem

You are given an integer array nums. You may reorder the array only if the elements you need to swap satisfy a specific bit-manipulation rule derived from their values.

Your task is to determine whether it is possible to sort the array in nondecreasing order using only the allowed reordering operations.

Return true if the array can be sorted under the rule, otherwise return false.

Intuition

This problem is about checking whether the array can be globally sorted while respecting a local constraint based on the binary structure of the numbers. A direct full sort is not enough; you need to reason about which values can move past each other under the restriction.

Input Format

  • A single integer array nums.
  • The array contains one or more integers.

Output Format

  • Return true if nums can be sorted in nondecreasing order under the given restriction.
  • Otherwise return false.

Constraints

  • 1 <= nums.length
  • Values are integers within a typical 32-bit signed range.
  • The exact operation rule is problem-specific and should be inferred from the statement.
Examples
Sample cases returned by the problem API.

Example 1

Input

nums = [8,4,2,30,15]

Output

true

Explanation

The array can be arranged into nondecreasing order while respecting the constraint, so the answer is true.

Example 2

Input

nums = [1,2,3,4,5]

Output

true

Explanation

The array is already sorted, so it is trivially sortable.

Show 1 more example

Example 3

Input

nums = [3,16,8,4,2]

Output

false

Explanation

The constrained reordering is not sufficient to place all values into nondecreasing order.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.