|
| 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 \leq nums.length \leq 5 \times 10^4$ |
| 38 | +- $-10^9 \leq nums[i] \leq 10^9$ |
| 39 | + |
| 40 | +### Approach |
| 41 | + |
| 42 | +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: |
| 43 | + |
| 44 | +1. **First Pass**: |
| 45 | + - Use two counters and two candidate variables to identify the potential majority elements. |
| 46 | + - Iterate through the array, updating the candidates and their counts accordingly. |
| 47 | + |
| 48 | +2. **Second Pass**: |
| 49 | + - Verify the candidates by counting their occurrences in the array. |
| 50 | + |
| 51 | +### Solution |
| 52 | + |
| 53 | +#### Code in Different Languages |
| 54 | + |
| 55 | +<Tabs> |
| 56 | +<TabItem value="cpp" label="C++"> |
| 57 | +<SolutionAuthor name="@Vipullakum007"/> |
| 58 | +```cpp |
| 59 | +#include <vector> |
| 60 | +#include <iostream> |
| 61 | + |
| 62 | +using namespace std; |
| 63 | + |
| 64 | +vector<int> majorityElement(vector<int>& nums) { |
| 65 | + int n = nums.size(); |
| 66 | + if (n == 0) return {}; |
| 67 | + |
| 68 | + int candidate1 = 0, candidate2 = 1, count1 = 0, count2 = 0; |
| 69 | + |
| 70 | + // First pass to find potential candidates |
| 71 | + for (int num : nums) { |
| 72 | + if (num == candidate1) { |
| 73 | + count1++; |
| 74 | + } else if (num == candidate2) { |
| 75 | + count2++; |
| 76 | + } else if (count1 == 0) { |
| 77 | + candidate1 = num; |
| 78 | + count1 = 1; |
| 79 | + } else if (count2 == 0) { |
| 80 | + candidate2 = num; |
| 81 | + count2 = 1; |
| 82 | + } else { |
| 83 | + count1--; |
| 84 | + count2--; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // Second pass to confirm the candidates |
| 89 | + count1 = count2 = 0; |
| 90 | + for (int num : nums) { |
| 91 | + if (num == candidate1) count1++; |
| 92 | + if (num == candidate2) count2++; |
| 93 | + } |
| 94 | + |
| 95 | + vector<int> result; |
| 96 | + if (count1 > n / 3) result.push_back(candidate1); |
| 97 | + if (count2 > n / 3) result.push_back(candidate2); |
| 98 | + |
| 99 | + return result; |
| 100 | +} |
| 101 | + |
| 102 | +int main() { |
| 103 | + vector<int> nums = {3,2,3}; |
| 104 | + vector<int> result = majorityElement(nums); |
| 105 | + for (int num : result) { |
| 106 | + cout << num << " "; |
| 107 | + } |
| 108 | +} |
| 109 | +</TabItem> |
| 110 | +<TabItem value="java" label="Java"> |
| 111 | +<SolutionAuthor name="@Vipullakum007"/> |
| 112 | +import java.util.*; |
| 113 | + |
| 114 | +public class MajorityElementII { |
| 115 | + public static List<Integer> majorityElement(int[] nums) { |
| 116 | + int n = nums.length; |
| 117 | + if (n == 0) return Collections.emptyList(); |
| 118 | + |
| 119 | + int candidate1 = 0, candidate2 = 1, count1 = 0, count2 = 0; |
| 120 | + |
| 121 | + // First pass to find potential candidates |
| 122 | + for (int num : nums) { |
| 123 | + if (num == candidate1) { |
| 124 | + count1++; |
| 125 | + } else if (num == candidate2) { |
| 126 | + count2++; |
| 127 | + } else if (count1 == 0) { |
| 128 | + candidate1 = num; |
| 129 | + count1 = 1; |
| 130 | + } else if (count2 == 0) { |
| 131 | + candidate2 = num; |
| 132 | + count2 = 1; |
| 133 | + } else { |
| 134 | + count1--; |
| 135 | + count2--; |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + // Second pass to confirm the candidates |
| 140 | + count1 = count2 = 0; |
| 141 | + for (int num : nums) { |
| 142 | + if (num == candidate1) count1++; |
| 143 | + if (num == candidate2) count2++; |
| 144 | + } |
| 145 | + |
| 146 | + List<Integer> result = new ArrayList<>(); |
| 147 | + if (count1 > n / 3) result.add(candidate1); |
| 148 | + if (count2 > n / 3) result.add(candidate2); |
| 149 | + |
| 150 | + return result; |
| 151 | + } |
| 152 | + |
| 153 | + public static void main(String[] args) { |
| 154 | + int[] nums = {3, 2, 3}; |
| 155 | + List<Integer> result = majorityElement(nums); |
| 156 | + System.out.println(result); |
| 157 | + } |
| 158 | +} |
| 159 | +</TabItem> |
| 160 | +<TabItem value="python" label="Python"> |
| 161 | +<SolutionAuthor name="@Vipullakum007"/> |
| 162 | +def majorityElement(nums): |
| 163 | + n = len(nums) |
| 164 | + if n == 0: |
| 165 | + return [] |
| 166 | + |
| 167 | + candidate1, candidate2, count1, count2 = 0, 1, 0, 0 |
| 168 | + |
| 169 | + # First pass to find potential candidates |
| 170 | + for num in nums: |
| 171 | + if num == candidate1: |
| 172 | + count1 += 1 |
| 173 | + elif num == candidate2: |
| 174 | + count2 += 1 |
| 175 | + elif count1 == 0: |
| 176 | + candidate1 = num |
| 177 | + count1 = 1 |
| 178 | + elif count2 == 0: |
| 179 | + candidate2 = num |
| 180 | + count2 = 1 |
| 181 | + else: |
| 182 | + count1 -= 1 |
| 183 | + count2 -= 1 |
| 184 | + |
| 185 | + # Second pass to confirm the candidates |
| 186 | + count1, count2 = 0, 0 |
| 187 | + for num in nums: |
| 188 | + if num == candidate1: |
| 189 | + count1 += 1 |
| 190 | + elif num == candidate2: |
| 191 | + count2 += 1 |
| 192 | + |
| 193 | + result = [] |
| 194 | + if count1 > n / 3: |
| 195 | + result.append(candidate1) |
| 196 | + if count2 > n / 3: |
| 197 | + result.append(candidate2) |
| 198 | + |
| 199 | + return result |
| 200 | + |
| 201 | +nums = [3, 2, 3] |
| 202 | +print(majorityElement(nums)) |
| 203 | +</TabItem> |
| 204 | +</Tabs> |
| 205 | + |
| 206 | +Complexity Analysis |
| 207 | + |
| 208 | +Time Complexity: O(N) |
| 209 | +Reason: We perform two passes through the array, each requiring linear time. |
| 210 | + |
| 211 | +Space Complexity: O(1) |
| 212 | +Reason: We use a constant amount of extra space for counters and candidates. |
| 213 | + |
| 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. |
| 215 | + |
| 216 | +References |
| 217 | +LeetCode Problem: Majority Element II |
| 218 | +Authors GeeksforGeeks Profile: Vipul lakum |
0 commit comments