diff --git a/problems/1356-sort-integers-by-the-number-of-1-bits/analysis.md b/problems/1356-sort-integers-by-the-number-of-1-bits/analysis.md new file mode 100644 index 0000000..a7a24cd --- /dev/null +++ b/problems/1356-sort-integers-by-the-number-of-1-bits/analysis.md @@ -0,0 +1,47 @@ +# 1356. Sort Integers by The Number of 1 Bits + +[LeetCode Link](https://leetcode.com/problems/sort-integers-by-the-number-of-1-bits/) + +Difficulty: Easy +Topics: Array, Bit Manipulation, Sorting, Counting +Acceptance Rate: 79.5% + +## Hints + +### Hint 1 + +This problem combines two fundamental concepts: **sorting with a custom comparator** and **counting bits**. Think about how you can define an ordering that considers two properties of each number at once. + +### Hint 2 + +You can use a custom sort where the primary key is the **popcount** (number of set bits) and the secondary key is the **value itself**. Go's `sort.Slice` lets you define exactly this kind of comparator. For counting bits, consider `math/bits.OnesCount`. + +### Hint 3 + +The comparator logic is straightforward: for two elements `a` and `b`, first compare `OnesCount(a)` vs `OnesCount(b)`. If they're equal, break the tie by comparing `a` vs `b` directly. That's the entire algorithm — the rest is just plugging it into a sort call. + +## Approach + +1. **Custom sort with two-level comparison**: We sort the input array in-place using `sort.Slice` with a comparator function. +2. **Primary key — bit count**: For each pair of elements, count the number of `1` bits in their binary representations using `bits.OnesCount`. +3. **Secondary key — value**: When two numbers have the same bit count, the smaller number comes first. + +Walking through Example 1 with `arr = [0,1,2,3,4,5,6,7,8]`: +- `0` → 0 bits, `1,2,4,8` → 1 bit each, `3,5,6` → 2 bits each, `7` → 3 bits +- Group by bit count, sort within each group by value: `[0, 1,2,4,8, 3,5,6, 7]` + +This is essentially a **stable sort by popcount** with value as tiebreaker, which the comparator handles naturally. + +## Complexity Analysis + +Time Complexity: O(n log n) — dominated by the sort. Each comparison is O(1) since `OnesCount` runs in constant time for bounded integers. + +Space Complexity: O(log n) — for the sort's internal stack. No auxiliary data structures are allocated. + +## Edge Cases + +- **Single element**: Array of length 1 is already sorted; the sort is a no-op. +- **All same bit count** (e.g., all powers of 2): Falls back to sorting by value, producing ascending order. +- **Zero in the array**: `0` has 0 set bits, so it always appears first. +- **All identical elements**: Already sorted; comparator returns `false` for both directions, preserving order. +- **Maximum constraint values**: `arr[i]` up to 10^4 (14 bits) — `OnesCount` handles this trivially. diff --git a/problems/1356-sort-integers-by-the-number-of-1-bits/problem.md b/problems/1356-sort-integers-by-the-number-of-1-bits/problem.md new file mode 100644 index 0000000..a752392 --- /dev/null +++ b/problems/1356-sort-integers-by-the-number-of-1-bits/problem.md @@ -0,0 +1,53 @@ +--- +number: "1356" +frontend_id: "1356" +title: "Sort Integers by The Number of 1 Bits" +slug: "sort-integers-by-the-number-of-1-bits" +difficulty: "Easy" +topics: + - "Array" + - "Bit Manipulation" + - "Sorting" + - "Counting" +acceptance_rate: 7952.3 +is_premium: false +created_at: "2026-02-25T03:20:39.915392+00:00" +fetched_at: "2026-02-25T03:20:39.915392+00:00" +link: "https://leetcode.com/problems/sort-integers-by-the-number-of-1-bits/" +date: "2026-02-25" +--- + +# 1356. Sort Integers by The Number of 1 Bits + +You are given an integer array `arr`. Sort the integers in the array in ascending order by the number of `1`'s in their binary representation and in case of two or more integers have the same number of `1`'s you have to sort them in ascending order. + +Return _the array after sorting it_. + + + +**Example 1:** + + + **Input:** arr = [0,1,2,3,4,5,6,7,8] + **Output:** [0,1,2,4,8,3,5,6,7] + **Explantion:** [0] is the only integer with 0 bits. + [1,2,4,8] all have 1 bit. + [3,5,6] have 2 bits. + [7] has 3 bits. + The sorted array by bits is [0,1,2,4,8,3,5,6,7] + + +**Example 2:** + + + **Input:** arr = [1024,512,256,128,64,32,16,8,4,2,1] + **Output:** [1,2,4,8,16,32,64,128,256,512,1024] + **Explantion:** All integers have 1 bit in the binary representation, you should just sort them in ascending order. + + + + +**Constraints:** + + * `1 <= arr.length <= 500` + * `0 <= arr[i] <= 104` diff --git a/problems/1356-sort-integers-by-the-number-of-1-bits/solution_daily_20260225.go b/problems/1356-sort-integers-by-the-number-of-1-bits/solution_daily_20260225.go new file mode 100644 index 0000000..d179cec --- /dev/null +++ b/problems/1356-sort-integers-by-the-number-of-1-bits/solution_daily_20260225.go @@ -0,0 +1,20 @@ +package main + +import ( + "math/bits" + "sort" +) + +// sortByBits sorts integers by the number of 1-bits in their binary +// representation (ascending), breaking ties by value (ascending). +// Uses sort.Slice with a two-level custom comparator. +func sortByBits(arr []int) []int { + sort.Slice(arr, func(i, j int) bool { + bi, bj := bits.OnesCount(uint(arr[i])), bits.OnesCount(uint(arr[j])) + if bi != bj { + return bi < bj + } + return arr[i] < arr[j] + }) + return arr +} diff --git a/problems/1356-sort-integers-by-the-number-of-1-bits/solution_daily_20260225_test.go b/problems/1356-sort-integers-by-the-number-of-1-bits/solution_daily_20260225_test.go new file mode 100644 index 0000000..4ccefd7 --- /dev/null +++ b/problems/1356-sort-integers-by-the-number-of-1-bits/solution_daily_20260225_test.go @@ -0,0 +1,57 @@ +package main + +import ( + "reflect" + "testing" +) + +func TestSortByBits(t *testing.T) { + tests := []struct { + name string + input []int + expected []int + }{ + { + name: "example 1: mixed bit counts 0-8", + input: []int{0, 1, 2, 3, 4, 5, 6, 7, 8}, + expected: []int{0, 1, 2, 4, 8, 3, 5, 6, 7}, + }, + { + name: "example 2: powers of two descending", + input: []int{1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1}, + expected: []int{1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024}, + }, + { + name: "edge case: single element", + input: []int{42}, + expected: []int{42}, + }, + { + name: "edge case: all same value", + input: []int{7, 7, 7}, + expected: []int{7, 7, 7}, + }, + { + name: "edge case: zero only", + input: []int{0}, + expected: []int{0}, + }, + { + name: "edge case: same bit count different values", + input: []int{3, 5, 6, 9, 10, 12}, + expected: []int{3, 5, 6, 9, 10, 12}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + // copy input to avoid mutating test data for debugging + inp := make([]int, len(tt.input)) + copy(inp, tt.input) + result := sortByBits(inp) + if !reflect.DeepEqual(result, tt.expected) { + t.Errorf("got %v, want %v", result, tt.expected) + } + }) + } +}