Skip to content

Commit 0924a7e

Browse files
chore: add LeetCode daily solution
1 parent d74dcc8 commit 0924a7e

File tree

5 files changed

+63
-0
lines changed

5 files changed

+63
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Binary Prefix Divisible By 5 (Easy)
2+
3+
**Problem ID:** 1018
4+
**Date:** 2025-11-24
5+
**Link:** https://leetcode.com/problems/binary-prefix-divisible-by-5/
6+
7+
## Approach
8+
9+
To solve the problem of determining whether the binary prefixes of a given binary array are divisible by 5, we can adopt an efficient approach that avoids constructing large integers directly from binary representations.
10+
11+
### Approach:
12+
13+
1. **Understanding Binary Representation**: Each prefix can be interpreted as a binary number. For instance, the binary number represented by the array `nums[0..i]` can be computed incrementally. Instead of converting the entire prefix to a decimal number at each step, we can maintain a running total.
14+
15+
2. **Incremental Calculation**: As we iterate through the array, we can maintain a variable `current` that represents the decimal value of the binary number formed by the prefix `nums[0..i]`. We update this variable using the formula:
16+
\[
17+
\text{current} = (\text{current} \times 2 + \text{nums}[i]) \mod 5
18+
\]
19+
This allows us to keep track of the value modulo 5, which is sufficient for our divisibility check.
20+
21+
3. **Divisibility Check**: For each prefix, after updating `current`, we check if `current` is equal to 0. If it is, it means the binary number represented by the prefix is divisible by 5, and we append `true` to our result array; otherwise, we append `false`.
22+
23+
4. **Data Structures**: We will use a simple list to store the boolean results for each prefix.
24+
25+
5. **Complexity**: The time complexity of this approach is O(n), where n is the length of the input array `nums`, since we make a single pass through the array. The space complexity is O(n) for the output array.
26+
27+
### Summary:
28+
By leveraging the properties of binary numbers and modular arithmetic, we can efficiently determine the divisibility of binary prefixes by 5 without constructing large integers. This results in a straightforward and optimal solution to the problem.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public List<Boolean> prefixesDivBy5(int[] nums) {
3+
List<Boolean> result = new ArrayList<>();
4+
int current = 0;
5+
6+
for (int num : nums) {
7+
current = ((current << 1) + num) % 5;
8+
result.add(current == 0);
9+
}
10+
11+
return result;
12+
}
13+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var prefixesDivBy5 = function(nums) {
2+
let result = [];
3+
let current = 0;
4+
5+
for (let num of nums) {
6+
current = ((current << 1) + num) % 5;
7+
result.push(current === 0);
8+
}
9+
10+
return result;
11+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def prefixesDivBy5(self, nums: List[int]) -> List[bool]:
3+
result = []
4+
current = 0
5+
6+
for num in nums:
7+
current = (current << 1) | num
8+
result.append(current % 5 == 0)
9+
10+
return result

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,3 +288,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
288288
- 2025-11-21 — [Unique Length-3 Palindromic Subsequences](https://leetcode.com/problems/unique-length-3-palindromic-subsequences/) (Medium) → `Medium/2025-11-21-1930-Unique-Length-3-Palindromic-Subsequences`
289289
- 2025-11-22 — [Find Minimum Operations to Make All Elements Divisible by Three](https://leetcode.com/problems/find-minimum-operations-to-make-all-elements-divisible-by-three/) (Easy) → `Easy/2025-11-22-3190-Find-Minimum-Operations-to-Make-All-Elements-Divisible-by-Three`
290290
- 2025-11-23 — [Greatest Sum Divisible by Three](https://leetcode.com/problems/greatest-sum-divisible-by-three/) (Medium) → `Medium/2025-11-23-1262-Greatest-Sum-Divisible-by-Three`
291+
- 2025-11-24 — [Binary Prefix Divisible By 5](https://leetcode.com/problems/binary-prefix-divisible-by-5/) (Easy) → `Easy/2025-11-24-1018-Binary-Prefix-Divisible-By-5`

0 commit comments

Comments
 (0)