Advertisement

Find if Array Can Be Sorted - LeetCode 3011 Solution

Find if Array Can Be Sorted - Complete Solution Guide

Find if Array Can Be Sorted is LeetCode problem 3011, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

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 . Example 1: Input: nums = [8,4,2,30,15] Output: true Explanation: Let's look at the binary representation of every element. The numbers 2, 4, and 8 have one set bit each with binary rep

Detailed Explanation

The problem asks whether a given array of positive integers can be sorted into ascending order by repeatedly swapping adjacent elements that have the same number of set bits (i.e., the same number of 1s in their binary representation). The goal is to determine if a series of such swaps can result in a sorted array.

Solution Approach

The provided solution approach works by iterating through the input array and identifying contiguous groups of numbers with the same number of set bits. For each group, it finds the minimum and maximum values. It then checks if the minimum value of the current group is less than the maximum value of the previous group. If it is, it means that even with swaps within the groups with equal number of set bits, it's impossible to sort the array into ascending order because an element from the current group would need to be moved before the previous group to achieve sorting. The algorithm maintains the maximum value seen in the previous group and compares it with the minimum value of the current group of numbers with the same bit count.

Step-by-Step Algorithm

  1. Step 1: Initialize `prev_max` to 0. This variable will store the maximum value of the previous group of numbers having the same number of set bits.
  2. Step 2: Iterate through the `nums` array using a `while` loop controlled by the index `i`.
  3. Step 3: Inside the outer loop, determine the number of set bits for the current element `nums[i]` using the appropriate bit counting function (e.g., `bit_count` in C, `Integer.bitCount` in Java, `__builtin_popcount` in C++, `bit_count()` method in Python)
  4. Step 4: Initialize `curr_min` and `curr_max` to the current element `nums[i]`. These variables will store the minimum and maximum values within the current group.
  5. Step 5: Use an inner `while` loop (controlled by the index `j`) to traverse the contiguous elements that have the same number of set bits as `nums[i]`.
  6. Step 6: Within the inner loop, update `curr_min` and `curr_max` by comparing them with each element `nums[j]`.
  7. Step 7: After the inner loop finishes, check if `curr_min` is less than `prev_max`. If it is, it means that the array cannot be sorted using the allowed swaps, so return `false`.
  8. Step 8: Update `prev_max` to `curr_max` as we finish processing current group.
  9. Step 9: Increment the outer loop index `i` to `j`, effectively moving to the next group of numbers.
  10. Step 10: If the outer loop completes without returning `false`, it means that the array can be sorted using the allowed swaps, so return `true`.

Key Insights

  • Insight 1: The key is to recognize that numbers with the same number of set bits form independent groups that can be internally sorted, but cannot be intermixed with numbers from other groups.
  • Insight 2: The problem can be solved by iterating through the array and checking if the minimum value within each group of numbers with the same number of set bits is greater than or equal to the maximum value of the previous group.
  • Insight 3: No explicit sorting is required; the solution only needs to verify if sorting via adjacent swaps based on the set bits constraint is possible.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Array, Bit Manipulation, Sorting.

Companies

Asked at: Edelweiss Group.