-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMajorityElement.java
More file actions
232 lines (195 loc) · 6.78 KB
/
MajorityElement.java
File metadata and controls
232 lines (195 loc) · 6.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package Algorithms.DivideAndConquer;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* Most common element is different from majority element
* In most common element, we need to find the element which occurs the maximum number of times --> highest frequency
* but in majority element, we need to find the element which occurs more than n/2 times
* i.e when nums={1, 2, 2, 3, 3, 4, 5, 5, 2};
* the most common element is 2
* but we get different result for majority element --- cause, there is no majority element
*
* It can be solved using
* 1) Boyer-Moore Voting Algorithm
* 2) Divide and Conquer
* 3) HashMap
* 4) Sorting
* 5) Brute Force O(n^2)
* 6) Brute Force with HashSet
*
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 12 March 2025
* @link 169. Majority Element https://leetcode.com/problems/majority-element/
*/
public class MajorityElement {
public static void main(String[] args) {
int[] nums = {1, 2, 2, 3, 3, 4, 5, 5, 2};
System.out.println("majorityElement using Boyer-Moore Voting Algorithm => " + majorityElement(nums));
System.out.println("majorityElement using Divide and Conquer => " + majorityElementUsingDivideAndConquer(nums));
System.out.println("majorityElement using HashMap => " + majorityElementUsingHashMap(nums));
}
/**
* Boyer-Moore Voting Algorithm
*
* NOTE: Only one majority element in the array i.e majority element occurs more than n/2. So rest of the nums occurrence will be less than n/2
*
* Increase the count if we see majority element or decrease count if we don't see
* and if count==0 then change the majority element
*
* use if(count==0) condition before calculating count. Eg: [3,2,3]
*
* i=0
* [3, 2, 3]
* majority = 3, count = 1
*
* i=1
* [3, 2, 3]
* majority = 3, count = 0
* ---> here we don't change the majority element to 2 even if the count is 0.
* So, change the majority element value in next iteration if we didn't see 3 again
*
* i=2
* [3, 2, 3]
* majority = 3, count = 1
*
* @TimeComplexity O(N)
* @SpaceComplexity O(1)
*/
public static int majorityElement(int[] nums) {
int majority = nums[0], count = 0;
for(int num: nums) {
if(count == 0) majority = num;
count += num==majority? 1 : -1;
}
return majority;
}
public static int majorityElement2(int[] nums) {
int major = nums[0], count = 1;
for (int i = 1; i < nums.length; i++) {
if (nums[i] == major) count++;
else if (count == 0) {
major = nums[i];
count = 1;
}
else count--;
}
return major;
}
/**
* @TimeComplexity O(NlogN)
* @SpaceComplexity O(N)
*/
public static int majorityElementUsingDivideAndConquer(int[] nums) {
return majority(nums, 0, nums.length - 1);
}
private static int majority(int[] nums, int left, int right) {
if (left == right) return nums[left]; // base case
int mid = left + (right - left) / 2;
int leftMajor = majority(nums, left, mid);
int rightMajor = majority(nums, mid + 1, right);
if (leftMajor == rightMajor) {
return leftMajor;
}
// count the number of times leftMajor occurs in left side and rightMajor in right side
int leftCount = countInRange(nums, leftMajor, left, right);
int rightCount = countInRange(nums, rightMajor, left, right);
return leftCount > rightCount ? leftMajor : rightMajor;
}
private static int countInRange(int[] nums, int num, int left, int right) {
int count = 0;
for (int i = left; i <= right; i++) {
if (nums[i] == num) count++;
}
return count;
}
/**
* @TimeComplexity O(N)
* @SpaceComplexity O(N)
*
* Here we can use HashMap or Bucket Sort int[] with given range
*/
@SuppressWarnings("unused")
public static int majorityElementUsingHashMap(int[] nums) {
// Map<Integer, Integer> map = Arrays.stream(nums).boxed().collect(Collectors.groupingBy(i->i, Collectors.summingInt(e->1)));
Map<Integer, Integer> map = new HashMap<>(); // <num, numOccurrences>
for (int i: nums) map.merge(i, 1, Integer::sum);
int maxK=0, maxV=Integer.MIN_VALUE; // or java.util.Map.Entry<> majorityEntry = new java.util.AbstractMap.SimpleEntry<>(0, 0); and return majorityEntry.getKey();
for(Map.Entry<Integer, Integer> e: map.entrySet()) {
if (e.getValue() > nums.length/2) {
return e.getKey();
}
/**
or
if(e.getValue() > maxV) {
maxV=e.getValue();
maxK=e.getKey();
}
*/
}
return maxK;
}
/**
* @TimeComplexity O(nlogn)
* @SpaceComplexity O(1)
*/
public static int majorityElementUsingSort(int[] nums) {
Arrays.sort(nums);
return nums[nums.length/2];
}
public static int majorityElementUsingSort2(int[] nums) {
Arrays.sort(nums);
int majority = nums[0], count = 0;
for(int num: nums) {
if(majority==num) count++;
else {
majority = num;
count = 1;
}
if(count > nums.length/2) return majority;
}
return majority;
}
/**
* @TimeComplexity O(n^2)
* @SpaceComplexity O(1)
*/
public static int majorityElementUsingBruteForce(int[] nums) {
int n = nums.length;
int majority = nums[0], count = 0;
for(int num: nums) {
int currNum = num, currCount = 1;
for(int i=0; i<n; i++) {
if(nums[i]==num) currCount++;
}
if(currCount > count) {
count = currCount;
majority = currNum;
}
}
return majority;
}
/**
* Same as above brute force but use HashSet for O(1) lookup which we already calculated and skip it
*/
public static int majorityElementUsingBruteForce2(int[] nums) {
int n = nums.length;
int majority = nums[0], count = 0;
Set<Integer> seen = new HashSet<>();
for(int num: nums) {
int currNum = num, currCount = 1;
if(seen.contains(num)) continue;
seen.add(num);
for(int i=0; i<n; i++) {
if(nums[i]==num) currCount++;
}
if(currCount > count) {
count = currCount;
majority = currNum;
}
}
return majority;
}
}