Skip to content

Commit 020c965

Browse files
authored
Merge pull request #984 from nishant0708/Q229
[Feature Request]: Leetcode Problem 229 #933
2 parents 1497440 + 6477805 commit 020c965

File tree

1 file changed

+215
-0
lines changed

1 file changed

+215
-0
lines changed
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
---
2+
id: majority-element-II
3+
title: Majority Element II
4+
sidebar_label: 0229-Majority-Element-II
5+
tags:
6+
- Arrays
7+
- Counting
8+
- C++
9+
- Java
10+
- Python
11+
description: "This document provides a solution to the Majority Element II problem, where we need to find all elements that appear more than ⌊ n/3 ⌋ times."
12+
---
13+
14+
## Problem
15+
16+
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times.
17+
18+
### Examples
19+
20+
**Example 1:**
21+
22+
Input: nums = [3,2,3]
23+
Output: [3]
24+
25+
**Example 2:**
26+
27+
Input: nums = [1]
28+
Output: [1]
29+
30+
**Example 3:**
31+
32+
Input: nums = [1,2]
33+
Output: [1,2]
34+
35+
### Constraints
36+
37+
- `1 <= nums.length <= 5 * 10^4`
38+
- `-10^9 <= nums[i] <= 10^9`
39+
### Approach
40+
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:
42+
43+
1. **First Pass**:
44+
- Use two counters and two candidate variables to identify the potential majority elements.
45+
- Iterate through the array, updating the candidates and their counts accordingly.
46+
47+
2. **Second Pass**:
48+
- Verify the candidates by counting their occurrences in the array.
49+
50+
### Solution
51+
52+
#### Code in Different Languages
53+
54+
### C++ Solution
55+
```cpp
56+
#include <vector>
57+
#include <iostream>
58+
59+
using namespace std;
60+
61+
vector<int> majorityElement(vector<int>& nums) {
62+
int n = nums.size();
63+
if (n == 0) return {};
64+
65+
int candidate1 = 0, candidate2 = 1, count1 = 0, count2 = 0;
66+
67+
// First pass to find potential candidates
68+
for (int num : nums) {
69+
if (num == candidate1) {
70+
count1++;
71+
} else if (num == candidate2) {
72+
count2++;
73+
} else if (count1 == 0) {
74+
candidate1 = num;
75+
count1 = 1;
76+
} else if (count2 == 0) {
77+
candidate2 = num;
78+
count2 = 1;
79+
} else {
80+
count1--;
81+
count2--;
82+
}
83+
}
84+
85+
// Second pass to confirm the candidates
86+
count1 = count2 = 0;
87+
for (int num : nums) {
88+
if (num == candidate1) count1++;
89+
if (num == candidate2) count2++;
90+
}
91+
92+
vector<int> result;
93+
if (count1 > n / 3) result.push_back(candidate1);
94+
if (count2 > n / 3) result.push_back(candidate2);
95+
96+
return result;
97+
}
98+
99+
int main() {
100+
vector<int> nums = {3,2,3};
101+
vector<int> result = majorityElement(nums);
102+
for (int num : result) {
103+
cout << num << " ";
104+
}
105+
}
106+
```
107+
### Java Solution
108+
```java
109+
import java.util.*;
110+
111+
public class MajorityElementII {
112+
public static List<Integer> majorityElement(int[] nums) {
113+
int n = nums.length;
114+
if (n == 0) return Collections.emptyList();
115+
116+
int candidate1 = 0, candidate2 = 1, count1 = 0, count2 = 0;
117+
118+
// First pass to find potential candidates
119+
for (int num : nums) {
120+
if (num == candidate1) {
121+
count1++;
122+
} else if (num == candidate2) {
123+
count2++;
124+
} else if (count1 == 0) {
125+
candidate1 = num;
126+
count1 = 1;
127+
} else if (count2 == 0) {
128+
candidate2 = num;
129+
count2 = 1;
130+
} else {
131+
count1--;
132+
count2--;
133+
}
134+
}
135+
136+
// Second pass to confirm the candidates
137+
count1 = count2 = 0;
138+
for (int num : nums) {
139+
if (num == candidate1) count1++;
140+
if (num == candidate2) count2++;
141+
}
142+
143+
List<Integer> result = new ArrayList<>();
144+
if (count1 > n / 3) result.add(candidate1);
145+
if (count2 > n / 3) result.add(candidate2);
146+
147+
return result;
148+
}
149+
150+
public static void main(String[] args) {
151+
int[] nums = {3, 2, 3};
152+
List<Integer> result = majorityElement(nums);
153+
System.out.println(result);
154+
}
155+
}
156+
```
157+
### Python Solution
158+
159+
```python
160+
def majorityElement(nums):
161+
n = len(nums)
162+
if n == 0:
163+
return []
164+
165+
candidate1, candidate2, count1, count2 = 0, 1, 0, 0
166+
167+
# First pass to find potential candidates
168+
for num in nums:
169+
if num == candidate1:
170+
count1 += 1
171+
elif num == candidate2:
172+
count2 += 1
173+
elif count1 == 0:
174+
candidate1 = num
175+
count1 = 1
176+
elif count2 == 0:
177+
candidate2 = num
178+
count2 = 1
179+
else:
180+
count1 -= 1
181+
count2 -= 1
182+
183+
# Second pass to confirm the candidates
184+
count1, count2 = 0, 0
185+
for num in nums:
186+
if num == candidate1:
187+
count1 += 1
188+
elif num == candidate2:
189+
count2 += 1
190+
191+
result = []
192+
if count1 > n / 3:
193+
result.append(candidate1)
194+
if count2 > n / 3:
195+
result.append(candidate2)
196+
197+
return result
198+
199+
nums = [3, 2, 3]
200+
print(majorityElement(nums))
201+
```
202+
203+
### Complexity Analysis
204+
205+
### Time Complexity: O(N)
206+
>Reason: We perform two passes through the array, each requiring linear time.
207+
208+
**Space Complexity:** O(1)
209+
>Reason: We use a constant amount of extra space for counters and candidates.
210+
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.
212+
213+
#### References
214+
**LeetCode Problem:** Majority Element II
215+
**Authors GeeksforGeeks Profile:** Vipul lakum

0 commit comments

Comments
 (0)