Skip to content

3011. Find if Array Can Be Sorted #798

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We need to determine if the array can be sorted by only swapping adjacent elements that have the same number of set bits in their binary representation. Here’s the plan:

Solution Steps:

  1. Key Observation: The operation allows us to swap adjacent elements only if they have the same number of set bits. This restricts swapping across elements with different numbers of set bits.

  2. Plan:

    • Group elements by the number of set bits in their binary representation.
    • Sort each group individually, since within a group, elements can be rearranged by swaps.
    • After sorting each group, merge the sorted groups back together.
    • Check if this merged array is sorted. If it is, then sorting the array using the al…

Replies: 1 comment 2 replies

Comment options

mah-shamim
Nov 6, 2024
Maintainer Author

You must be logged in to vote
2 replies
@kovatz
Comment options

kovatz Nov 6, 2024
Collaborator

@mah-shamim
Comment options

mah-shamim Nov 6, 2024
Maintainer Author

Answer selected by kovatz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants