forked from dimpeshmalviya/C-Language-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMajorityII.c
More file actions
149 lines (129 loc) · 4.08 KB
/
MajorityII.c
File metadata and controls
149 lines (129 loc) · 4.08 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
// package _5_arrays.hard;
// Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times
// eg:- [1,1,1,3,3,2,2,2] , n=8
// O/P -> [1,2]
// n/3 = 2
// therefore the occurrence should be more than n/3 -> therefore in this case minimum should be 3 times
// 1 appears -> 3 times
// 2 appears -> 3 times
// and 3+3 = 6, remaining is 2
// and 2 <= n/3
// therefore in majority II question there will be at most 2 majority integers
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
// extreme brute force -> check every element in the array and traverse the entire array and check the appearance
// TC -> O(n^2)
// SC -> O(1)
void majorityElement_bruteforce(int arr[], int n) {
int ans[2]; // stores at most 2 majority elements
int ans_size = 0;
for (int i = 0; i < n; i++) {
int already_present = 0;
for (int k = 0; k < ans_size; k++) {
if (ans[k] == arr[i]) {
already_present = 1;
break;
}
}
if (already_present) continue;
int count = 0;
for (int j = 0; j < n; j++) {
if (arr[j] == arr[i]) count++;
}
if (count > n / 3) {
ans[ans_size++] = arr[i];
}
if (ans_size == 2) break;
}
printf("Brute Force Result: ");
for (int i = 0; i < ans_size; i++) printf("%d ", ans[i]);
printf("\n");
}
// better solution -> find the frequency and store it in hashmap or in an array
// for 1st case: TC -> O(n) + O(n) + O(1) -> avg or best case in unordered map
// for 2nd case: TC -> O(n) + O(1) -> avg or best case in unordered map
// SC -> O(n) + O(1)
void majorityElement_better(int arr[], int n) {
// Since C does not have a HashMap by default, we'll simulate one using arrays
int unique[1000];
int freq[1000];
int unique_count = 0;
int ans[2];
int ans_size = 0;
for (int i = 0; i < n; i++) {
int found = 0;
for (int j = 0; j < unique_count; j++) {
if (unique[j] == arr[i]) {
freq[j]++;
found = 1;
// if we don't want two O(n) loops to run -> we want to do code in one loop
if (freq[j] > n / 3) {
int already_in_ans = 0;
for (int k = 0; k < ans_size; k++) {
if (ans[k] == arr[i]) already_in_ans = 1;
}
if (!already_in_ans) ans[ans_size++] = arr[i];
}
break;
}
}
if (!found) {
unique[unique_count] = arr[i];
freq[unique_count] = 1;
unique_count++;
}
if (ans_size == 2) break;
}
printf("Better Solution Result: ");
for (int i = 0; i < ans_size; i++) printf("%d ", ans[i]);
printf("\n");
}
// Optimal solution
// TC -> O(N) + O(N)
// SC -> O(1)
void majorityElement_optimal(int arr[], int n) {
int cnt1 = 0;
int cnt2 = 0;
int el1 = INT_MIN;
int el2 = INT_MIN;
for (int i = 0; i < n; i++) {
if (cnt1 == 0 && el2 != arr[i]) {
cnt1 = 1;
el1 = arr[i];
}
else if (cnt2 == 0 && el1 != arr[i]) {
cnt2 = 1;
el2 = arr[i];
}
else if (el1 == arr[i]) cnt1++;
else if (el2 == arr[i]) cnt2++;
else {
cnt1--;
cnt2--;
}
}
// Reset counts
cnt1 = 0;
cnt2 = 0;
for (int i = 0; i < n; i++) {
if (arr[i] == el1) cnt1++;
if (arr[i] == el2) cnt2++;
}
printf("Optimal Solution Result: ");
if (cnt1 > n / 3) printf("%d ", el1);
if (cnt2 > n / 3 && el2 != el1) printf("%d ", el2);
printf("\n");
}
int main() {
// List<Integer> arr = new ArrayList<>();
// arr.addAll(Arrays.asList(1,1,1,3,3,2,2,2));
int arr[] = {1, 1, 1, 3, 3, 2, 2, 2};
int n = sizeof(arr) / sizeof(arr[0]);
// List<Integer> arr = List.of(1,1,1,3,3,2,2,2);
// Call all three versions
majorityElement_bruteforce(arr, n);
majorityElement_better(arr, n);
majorityElement_optimal(arr, n);
return 0;
}