Skip to content

Commit 005db8e

Browse files
Create 0927-Three-Equal-Parts.md
Add Solution to the lc problem 927 Three-Equal-Parts using python java cpp c and js
1 parent 428b287 commit 005db8e

File tree

1 file changed

+273
-0
lines changed

1 file changed

+273
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
1+
---
2+
id: Three-Equal-Parts
3+
title: Three-Equal-Parts
4+
sidebar_label: Three-Equal-Parts
5+
tags:
6+
- arrays
7+
- binary
8+
- partitioning
9+
---
10+
11+
## Problem Description
12+
13+
| Problem Statement | Solution Link | LeetCode Profile |
14+
| :------------------------------------------------------ | :------------------------------------------------------------------------- | :------------------------------------------------------ |
15+
| [Three-Equal-Parts](https://leetcode.com/problems/Three-Equal-Parts/description/) | [Three-Equal-Parts Solution on LeetCode](https://leetcode.com/problems/Three-Equal-Parts/solutions/) | [Nikita Saini](https://leetcode.com/u/Saini_Nikita/) |
16+
## Problem Description
17+
18+
You are given an array `arr` which consists of only zeros and ones. Divide the array into three non-empty parts such that all of these parts represent the same binary value.
19+
20+
If it is possible, return any `[i, j]` with `i + 1 < j`, such that:
21+
22+
- `arr[0], arr[1], ..., arr[i]` is the first part,
23+
- `arr[i + 1], arr[i + 2], ..., arr[j - 1]` is the second part, and
24+
- `arr[j], arr[j + 1], ..., arr[arr.length - 1]` is the third part.
25+
26+
All three parts should have equal binary values. If it is not possible, return `[-1, -1]`.
27+
28+
Note that the entire part is used when considering what binary value it represents. For example, `[1,1,0]` represents 6 in decimal, not 3. Also, leading zeros are allowed, so `[0,1,1]` and `[1,1]` represent the same value.
29+
30+
### Example 1
31+
32+
**Input:** `arr = [1,0,1,0,1]`
33+
**Output:** `[0,3]`
34+
35+
### Example 2
36+
37+
**Input:** `arr = [1,1,0,1,1]`
38+
**Output:** `[-1,-1]`
39+
40+
### Example 3
41+
42+
**Input:** `arr = [1,1,0,0,1]`
43+
**Output:** `[0,2]`
44+
45+
## Constraints
46+
47+
- `3 <= arr.length <= 3 * 10^4`
48+
- `arr[i]` is `0` or `1`.
49+
50+
## Approach
51+
52+
1. **Count the number of 1's in the array:**
53+
- If the total number of 1's is not divisible by 3, return `[-1, -1]`.
54+
- If the array is all zeros, return any valid split as `[0, n-1]` (since all parts will be 0).
55+
56+
2. **Identify target 1's in each part:**
57+
- The target number of 1's in each part is `total_ones // 3`.
58+
59+
3. **Find the starting index of each part:**
60+
- Locate the start of each part by finding the index where the 1's for each part begins.
61+
62+
4. **Check for matching parts:**
63+
- Ensure that the parts from the identified start indices match in terms of the sequence and trailing zeros.
64+
65+
5. **Return the valid indices if parts match:**
66+
- If valid, return the appropriate indices, otherwise return `[-1, -1]`.
67+
68+
## Step-by-Step Algorithm
69+
70+
1. Count the number of 1's in the array. If it's not divisible by 3, return `[-1, -1]`.
71+
2. If the array is all zeros, return `[0, arr.length - 1]`.
72+
3. Calculate the target number of 1's per part.
73+
4. Identify the starting indices for each part by counting the 1's until reaching the target for each part.
74+
5. Compare the parts to ensure they are identical.
75+
6. Return the indices if parts are identical, otherwise return `[-1, -1]`.
76+
77+
## Solution
78+
79+
### Python
80+
```python
81+
def threeEqualParts(arr):
82+
ones = sum(arr)
83+
if ones % 3 != 0:
84+
return [-1, -1]
85+
if ones == 0:
86+
return [0, len(arr) - 1]
87+
88+
k = ones // 3
89+
ones_seen = 0
90+
first = second = third = -1
91+
92+
for i in range(len(arr)):
93+
if arr[i] == 1:
94+
if ones_seen == 0:
95+
first = i
96+
elif ones_seen == k:
97+
second = i
98+
elif ones_seen == 2 * k:
99+
third = i
100+
ones_seen += 1
101+
102+
length = len(arr) - third
103+
if first + length <= second and second + length <= third:
104+
if arr[first:first + length] == arr[second:second + length] == arr[third:]:
105+
return [first + length - 1, second + length]
106+
107+
return [-1, -1]
108+
```
109+
110+
### Java
111+
```java
112+
public int[] threeEqualParts(int[] arr) {
113+
int ones = 0;
114+
for (int num : arr) {
115+
if (num == 1) ones++;
116+
}
117+
if (ones % 3 != 0) return new int[] {-1, -1};
118+
if (ones == 0) return new int[] {0, arr.length - 1};
119+
120+
int k = ones / 3;
121+
int first = -1, second = -1, third = -1;
122+
int count = 0;
123+
for (int i = 0; i < arr.length; i++) {
124+
if (arr[i] == 1) {
125+
if (count == 0) first = i;
126+
else if (count == k) second = i;
127+
else if (count == 2 * k) third = i;
128+
count++;
129+
}
130+
}
131+
132+
int len = arr.length - third;
133+
if (first + len <= second && second + len <= third) {
134+
for (int i = 0; i < len; i++) {
135+
if (arr[first + i] != arr[second + i] || arr[first + i] != arr[third + i]) {
136+
return new int[] {-1, -1};
137+
}
138+
}
139+
return new int[] {first + len - 1, second + len};
140+
}
141+
return new int[] {-1, -1};
142+
}
143+
```
144+
145+
### C++
146+
```cpp
147+
#include <vector>
148+
using namespace std;
149+
150+
vector<int> threeEqualParts(vector<int>& arr) {
151+
int ones = 0;
152+
for (int num : arr) {
153+
if (num == 1) ones++;
154+
}
155+
if (ones % 3 != 0) return {-1, -1};
156+
if (ones == 0) return {0, (int)arr.size() - 1};
157+
158+
int k = ones / 3;
159+
int first = -1, second = -1, third = -1;
160+
int count = 0;
161+
for (int i = 0; i < arr.size(); i++) {
162+
if (arr[i] == 1) {
163+
if (count == 0) first = i;
164+
else if (count == k) second = i;
165+
else if (count == 2 * k) third = i;
166+
count++;
167+
}
168+
}
169+
170+
int len = arr.size() - third;
171+
if (first + len <= second && second + len <= third) {
172+
for (int i = 0; i < len; i++) {
173+
if (arr[first + i] != arr[second + i] || arr[first + i] != arr[third + i]) {
174+
return {-1, -1};
175+
}
176+
}
177+
return {first + len - 1, second + len};
178+
}
179+
return {-1, -1};
180+
}
181+
```
182+
183+
### C
184+
```c
185+
#include <stdlib.h>
186+
187+
int* threeEqualParts(int* arr, int arrSize, int* returnSize) {
188+
int ones = 0;
189+
for (int i = 0; i < arrSize; i++) {
190+
if (arr[i] == 1) ones++;
191+
}
192+
int* result = (int*)malloc(2 * sizeof(int));
193+
if (ones % 3 != 0) {
194+
result[0] = -1;
195+
result[1] = -1;
196+
*returnSize = 2;
197+
return result;
198+
}
199+
if (ones == 0) {
200+
result[0] = 0;
201+
result[1] = arrSize - 1;
202+
*returnSize = 2;
203+
return result;
204+
}
205+
206+
int k = ones / 3;
207+
int first = -1, second = -1, third = -1;
208+
int count = 0;
209+
for (int i = 0; i < arrSize; i++) {
210+
if (arr[i] == 1) {
211+
if (count == 0) first = i;
212+
else if (count == k) second = i;
213+
else if (count == 2 * k) third = i;
214+
count++;
215+
}
216+
}
217+
218+
int len = arrSize - third;
219+
if (first + len <= second && second + len <= third) {
220+
for (int i = 0; i < len; i++) {
221+
if (arr[first + i] != arr[second + i] || arr[first + i] != arr[third + i]) {
222+
result[0] = -1;
223+
result[1] = -1;
224+
*returnSize = 2;
225+
return result;
226+
}
227+
}
228+
result[0] = first + len - 1;
229+
result[1] = second + len;
230+
*returnSize = 2;
231+
return result;
232+
}
233+
result[0] = -1;
234+
result[1] = -1;
235+
*returnSize = 2;
236+
return result;
237+
}
238+
```
239+
240+
### JavaScript
241+
```javascript
242+
var threeEqualParts = function(arr) {
243+
const ones = arr.reduce((a, b) => a + b, 0);
244+
if (ones % 3 !== 0) return [-1, -1];
245+
if (ones === 0) return [0, arr.length - 1];
246+
247+
const k = ones / 3;
248+
let first = -1, second = -1, third = -1, count = 0;
249+
for (let i = 0; i < arr.length; i++) {
250+
if (arr[i] === 1) {
251+
if (count === 0) first = i;
252+
else if (count === k) second = i;
253+
else if (count === 2 * k) third = i;
254+
count++;
255+
}
256+
}
257+
258+
const len = arr.length - third;
259+
if (first + len <= second && second + len <= third) {
260+
for (let i = 0; i < len; i++) {
261+
if (arr[first + i] !== arr[second + i] || arr[first + i] !== arr[third + i]) {
262+
return [-1, -1];
263+
}
264+
}
265+
return [first + len - 1, second + len];
266+
}
267+
return [-1, -1];
268+
};
269+
```
270+
271+
## Conclusion
272+
273+
This problem involves dividing an array of binary digits into three equal parts such that each part represents the same binary value. By counting the number of 1's and identifying the correct indices for each part, we can determine if such a division is possible and return the appropriate indices. The provided solutions in Python, Java, C++, C, and JavaScript offer various implementations of this approach.

0 commit comments

Comments
 (0)