Skip to content

Commit 9fba06f

Browse files
chore: add LeetCode daily solution
1 parent a7279da commit 9fba06f

5 files changed

Lines changed: 74 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Binary Gap (Easy)
2+
3+
**Problem ID:** 868
4+
**Date:** 2026-02-22
5+
**Link:** https://leetcode.com/problems/binary-gap/
6+
7+
## Approach
8+
9+
To solve the "Binary Gap" problem, we need to find the longest distance between any two adjacent '1's in the binary representation of a given positive integer \( n \).
10+
11+
### Approach:
12+
13+
1. **Binary Representation**: First, we convert the integer \( n \) into its binary form. This can be done using built-in functions or bit manipulation.
14+
15+
2. **Track Positions of '1's**: As we traverse the binary string, we will keep track of the positions (indices) of each '1' encountered. This can be done using a simple list or array to store the indices.
16+
17+
3. **Calculate Distances**: Once we have the positions of all '1's, we can calculate the distances between each pair of adjacent '1's. The distance between two '1's at positions \( i \) and \( j \) is given by \( j - i \).
18+
19+
4. **Find Maximum Distance**: As we compute the distances, we maintain a variable to track the maximum distance found. If there are no adjacent '1's (i.e., less than two '1's in the binary representation), we return 0.
20+
21+
### Data Structures:
22+
- A list (or array) to store the indices of '1's in the binary representation.
23+
24+
### Complexity:
25+
- **Time Complexity**: The time complexity is \( O(\log n) \) because the number of bits in the binary representation of \( n \) is \( \log_2(n) \).
26+
- **Space Complexity**: The space complexity is also \( O(\log n) \) due to storing the indices of '1's.
27+
28+
This approach efficiently calculates the desired distance by leveraging the properties of binary numbers and simple list operations, ensuring that we meet the problem's constraints.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int binaryGap(int n) {
3+
int lastPosition = -1;
4+
int maxGap = 0;
5+
6+
for (int i = 0; i < 32; i++) {
7+
if ((n & (1 << i)) != 0) {
8+
if (lastPosition != -1) {
9+
maxGap = Math.max(maxGap, i - lastPosition);
10+
}
11+
lastPosition = i;
12+
}
13+
}
14+
15+
return maxGap;
16+
}
17+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var binaryGap = function(n) {
2+
let lastIndex = -1;
3+
let maxDistance = 0;
4+
5+
for (let i = 0; n > 0; i++) {
6+
if (n & 1) {
7+
if (lastIndex !== -1) {
8+
maxDistance = Math.max(maxDistance, i - lastIndex);
9+
}
10+
lastIndex = i;
11+
}
12+
n >>= 1;
13+
}
14+
15+
return maxDistance;
16+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def binaryGap(self, n: int) -> int:
3+
last_position = -1
4+
max_gap = 0
5+
6+
for i in range(32):
7+
if n & (1 << i):
8+
if last_position != -1:
9+
max_gap = max(max_gap, i - last_position)
10+
last_position = i
11+
12+
return max_gap

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,3 +372,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
372372
- 2026-02-19 — [Count Binary Substrings](https://leetcode.com/problems/count-binary-substrings/) (Easy) → `Easy/2026-02-19-696-Count-Binary-Substrings`
373373
- 2026-02-20 — [Special Binary String](https://leetcode.com/problems/special-binary-string/) (Hard) → `Hard/2026-02-20-761-Special-Binary-String`
374374
- 2026-02-21 — [Prime Number of Set Bits in Binary Representation](https://leetcode.com/problems/prime-number-of-set-bits-in-binary-representation/) (Easy) → `Easy/2026-02-21-762-Prime-Number-of-Set-Bits-in-Binary-Representation`
375+
- 2026-02-22 — [Binary Gap](https://leetcode.com/problems/binary-gap/) (Easy) → `Easy/2026-02-22-868-Binary-Gap`

0 commit comments

Comments
 (0)