You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: dsa-solutions/lc-solutions/0200-0299/0229-Majority-Element.md
+14-18Lines changed: 14 additions & 18 deletions
Original file line number
Diff line number
Diff line change
@@ -34,9 +34,8 @@ Output: [1,2]
34
34
35
35
### Constraints
36
36
37
-
- $1 \leq nums.length \leq 5 \times 10^4$
38
-
- $-10^9 \leq nums[i] \leq 10^9$
39
-
37
+
-`1 <= nums.length <= 5 * 10^4`
38
+
-`-10^9 <= nums[i] <= 10^9`
40
39
### Approach
41
40
42
41
To solve this problem, we can use the Boyer-Moore Voting Algorithm, which efficiently finds the majority elements in linear time and constant space. The algorithm can be summarized in the following steps:
@@ -52,9 +51,7 @@ To solve this problem, we can use the Boyer-Moore Voting Algorithm, which effici
52
51
53
52
#### Code in Different Languages
54
53
55
-
<Tabs>
56
-
<TabItemvalue="cpp"label="C++">
57
-
<SolutionAuthorname="@Vipullakum007"/>
54
+
### C++ Solution
58
55
```cpp
59
56
#include<vector>
60
57
#include<iostream>
@@ -106,9 +103,9 @@ int main() {
106
103
cout << num << " ";
107
104
}
108
105
}
109
-
</TabItem>
110
-
<TabItemvalue="java"label="Java">
111
-
<SolutionAuthorname="@Vipullakum007"/>
106
+
```
107
+
### Java Solution
108
+
```java
112
109
import java.util.*;
113
110
114
111
public class MajorityElementII {
@@ -156,9 +153,10 @@ public class MajorityElementII {
156
153
System.out.println(result);
157
154
}
158
155
}
159
-
</TabItem>
160
-
<TabItemvalue="python"label="Python">
161
-
<SolutionAuthorname="@Vipullakum007"/>
156
+
```
157
+
### Python Solution
158
+
159
+
```python
162
160
defmajorityElement(nums):
163
161
n =len(nums)
164
162
if n ==0:
@@ -200,20 +198,18 @@ def majorityElement(nums):
200
198
201
199
nums = [3, 2, 3]
202
200
print(majorityElement(nums))
203
-
</TabItem>
204
-
</Tabs>
201
+
```
205
202
206
203
### Complexity Analysis
207
204
208
205
### Time Complexity: O(N)
209
-
**Reason:** We perform two passes through the array, each requiring linear time.
206
+
>Reason: We perform two passes through the array, each requiring linear time.
210
207
211
208
**Space Complexity:** O(1)
212
-
**Reason:** We use a constant amount of extra space for counters and candidates.
209
+
>Reason: We use a constant amount of extra space for counters and candidates.
213
210
214
-
This solution efficiently finds all elements that appear more than ⌊ n/3 ⌋ times using the Boyer-Moore Voting Algorithm. The time complexity is linear, and the space complexity is constant, making it suitable for large input sizes.
211
+
>This solution efficiently finds all elements that appear more than ⌊ n/3 ⌋ times using the Boyer-Moore Voting Algorithm. The time complexity is linear, and the space complexity is constant, making it suitable for large input sizes.
0 commit comments