← Home

1356. Sort Integers by The Number of 1 Bits

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
bit manipulationC++Markdown
135

This problem looks busy at first, but the accepted solution is built around one steady invariant. For 1356. Sort Integers by The Number of 1 Bits, the solution in this repository is mainly a bit manipulation solution.

Guide

What?

Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: bit manipulation, greedy.

Guide

When?

Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are count_set_bit, sortByBits.

Guide

Why?

The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.

  • Sorting is used to make local choices comparable, so the later scan does not have to rediscover order.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Start from the smallest reliable state.
  2. Expand one legal move at a time.
  3. Cache, count, or merge information as soon as it becomes settled.
  4. Let the final stored value answer the original question.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 12 ms, faster than 100.00% of C++ online submissions for Sort Integers by The Number of 1 Bits.
02//Memory Usage: 9.5 MB, less than 100.00% of C++ online submissions for Sort Integers by The Number of 1 Bits.
03
04class Solution {
05public:
06    int count_set_bit(int x){
07        int bits = 0;
08        while(x != 0){
09            bits += x&1;
10            x >>= 1;
11        }
12        return bits;
13    }
14    vector<int> sortByBits(vector<int>& arr) {
15        vector<int> sorted;
16        for(int e : arr){
17            //first sort by set bit count and then by itself
18            //* 100000 because the max of arr is 10000
19            sorted.push_back(count_set_bit(e) * 100000 + e);
20        }
21        sort(sorted.begin(), sorted.end());
22        for(int i = 0; i < sorted.size(); i++){
23            sorted[i] = sorted[i]%100000;
24        }
25        
26        return sorted;
27    }
28};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.