-
Notifications
You must be signed in to change notification settings - Fork 1
/
2June(IV).java
93 lines (69 loc) · 2.59 KB
/
2June(IV).java
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
public class MajorityElement {
// brute froece appraoach -- compare the current element with the other elements of the array
// TC = O(n^2) .... time limit exceeding method...
static int approach1(int[] nums){
int n = nums.length;
int majorityCount = n/2; // the element should have apperaed more than n/2 times in array
for(int num : nums){
int count = 0;
for(int elem : nums){
if(elem == num){
count+=1;
}
}
if(count>majorityCount){
return num;
}
}
return 0;
}
// approach 2 - hashmap
static void approach2(int[] nums){
}
// appraoch 3 - Boyer moore Algorithm (basically vote algorithm- used in string questions)
// if the same element come increment count ... if other element apper decrement the count
// TC = O(n) and SC = O(1)
// Algo -
/*
1 - We maintain a count
2 - count is incremented whenever we see an instance of our array current element is equal to majority element
3- else we decrement the count
*/
static int appraoch3(int[] nums){
int n = nums.length;
int count =0;
int majorityElement = 0;
for(int i =0;i<n;i++){
// whenever count == 0 we make a new majority element
if(count==0){ // counter reset you can say
majorityElement = nums[i];
count++;
}
else if(majorityElement==nums[i]){
count++;
}
else{
count--;
}
}
// now to we have to check what's the last element inside the majorityElement ...
// and verify if it's lenght is greater than n/2
// count = 0;
// for(int i = 0 ; i<n ; i++){
// if(majorityElement==nums[i]){
// count++;
// }
// }
// if will run without the commented code
// because we assume that the majority element always exists in the array.
// int ans = count>n/2?majorityElement:0;
// return ans;
return majorityElement;
}
public static void main(String[] args) {
int[] arr = {2,2,1,1,1,2,2};
System.out.println(approach1(arr));
}
}
// output :
// 3