Skip to content

Commit 47b5df2

Browse files
authored
Merge pull request #4157 from ImmidiSivani/leetcode-1018
solution added to 1018
2 parents a805327 + 3030669 commit 47b5df2

File tree

1 file changed

+134
-0
lines changed

1 file changed

+134
-0
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
---
2+
id: binary-prefix-divisible-by-5
3+
title: Binary Prefix Divisible By 5
4+
sidebar_label: Binary Prefix Divisible By 5
5+
tags:
6+
- Array
7+
- Bit Manipulation
8+
- LeetCode
9+
- Java
10+
- Python
11+
- C++
12+
description: "This is a solution to the Binary Prefix Divisible By 5 problem on LeetCode."
13+
sidebar_position: 25
14+
---
15+
16+
## Problem Description
17+
18+
You are given a binary array `nums` (0-indexed).
19+
20+
We define `xi` as the number whose binary representation is the subarray `nums[0..i]` (from most-significant-bit to least-significant-bit).
21+
22+
For example, if `nums = [1,0,1]`, then `x0 = 1`, `x1 = 2`, and `x2 = 5`.
23+
24+
Return an array of booleans `answer` where `answer[i]` is true if `xi` is divisible by 5.
25+
26+
### Examples
27+
28+
**Example 1:**
29+
30+
```
31+
Input: nums = [0,1,1]
32+
Output: [true,false,false]
33+
Explanation: The input numbers in binary are 0, 01, 011; which are 0, 1, and 3 in base-10.
34+
Only the first number is divisible by 5, so answer[0] is true.
35+
```
36+
37+
**Example 2:**
38+
39+
```
40+
Input: nums = [1,1,1]
41+
Output: [false,false,false]
42+
```
43+
44+
### Constraints
45+
46+
- `1 <= nums.length <= 10^5`
47+
- `nums[i]` is either 0 or 1.
48+
49+
---
50+
51+
## Solution for Binary Prefix Divisible By 5 Problem
52+
53+
To solve this problem, we can take advantage of the fact that we only need to check divisibility by 5. As we iterate through the array, we can keep track of the current number modulo 5. This allows us to avoid handling potentially large integers.
54+
55+
### Approach
56+
57+
1. Initialize current to 0, which will store the current number modulo 5.
58+
2. Iterate through each element in nums:
59+
- Update current by shifting it left and adding the current element.
60+
- Take current modulo 5 to keep it within manageable range.
61+
- Append True to the result if current is 0, otherwise append False.
62+
63+
### Code in Different Languages
64+
65+
<Tabs>
66+
<TabItem value="C++" label="C++" default>
67+
<SolutionAuthor name="@ImmidiSivani"/>
68+
69+
```cpp
70+
class Solution {
71+
public:
72+
vector<bool> prefixesDivBy5(vector<int>& nums) {
73+
vector<bool> result;
74+
int current = 0;
75+
for (int num : nums) {
76+
current = (current * 2 + num) % 5;
77+
result.push_back(current == 0);
78+
}
79+
return result;
80+
}
81+
};
82+
```
83+
84+
</TabItem>
85+
<TabItem value="Java" label="Java">
86+
<SolutionAuthor name="@ImmidiSivani"/>
87+
88+
```java
89+
class Solution {
90+
public List<Boolean> prefixesDivBy5(int[] nums) {
91+
List<Boolean> result = new ArrayList<>();
92+
int current = 0;
93+
for (int num : nums) {
94+
current = (current * 2 + num) % 5;
95+
result.add(current == 0);
96+
}
97+
return result;
98+
}
99+
}
100+
```
101+
102+
</TabItem>
103+
<TabItem value="Python" label="Python">
104+
<SolutionAuthor name="@ImmidiSivani"/>
105+
106+
```python
107+
class Solution:
108+
def prefixesDivBy5(self, nums: List[int]) -> List[bool]:
109+
result = []
110+
current = 0
111+
for num in nums:
112+
current = (current * 2 + num) % 5
113+
result.append(current == 0)
114+
return result
115+
```
116+
117+
</TabItem>
118+
</Tabs>
119+
120+
#### Complexity Analysis
121+
122+
- **Time Complexity**: $O(n)$, where `n` is the length of the array. We iterate through the array once.
123+
- **Space Complexity**: $O(1)$, not including the space needed for the output array. We only use a constant amount of additional space.
124+
125+
---
126+
127+
<h2>Authors:</h2>
128+
129+
<div style={{display: 'flex', flexWrap: 'wrap', justifyContent: 'space-between', gap: '10px'}}>
130+
{['ImmidiSivani'].map(username => (
131+
<Author key={username} username={username} />
132+
))}
133+
</div>
134+
```

0 commit comments

Comments
 (0)