-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmajoritiesinarrays.cpp
161 lines (133 loc) · 3.39 KB
/
majoritiesinarrays.cpp
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
// {"category": "Algorithm", "notes": "Find majority element in array"}
#include <SDKDDKVer.h>
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <vector>
#include <windows.h>
using namespace std;
//------------------------------------------------------------------------------
//
// Find the majority element in an array if exists. The majority is an element
// that occurs for more than half of the size of the array.
//
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//
// Implementation
//
//------------------------------------------------------------------------------
bool isMajorityElement(
int element,
const vector<int>& intArray)
{
vector<int>::size_type count = 0;
for (auto& each : intArray)
{
count += (each == element) ? 1 : 0;
}
return (count * 2 > intArray.size());
}
int findMajorityElement(
const vector<int>& intArray)
{
int majorityElement;
int count = 0;
if (intArray.size() == 0)
{
throw exception();
}
for (auto& each : intArray)
{
if (0 == count)
{
majorityElement = each;
count = 1;
}
else if (each == majorityElement)
{
++count;
}
else
{
--count;
}
}
if (!isMajorityElement(majorityElement, intArray))
{
throw exception();
}
return majorityElement;
}
//------------------------------------------------------------------------------
//
// Unit tests
//
//------------------------------------------------------------------------------
int _tmain(
int argc,
_TCHAR* argv[])
{
struct TestCase
{
vector<int> intArray;
int majorityElement;
bool exceptionExpected;
};
const TestCase tests[] =
{
{ vector<int>{{1, 2, 3, 2, 2, 2, 5, 4, 2}}, 2, false },
{ vector<int>{{1, 2, 3, 2, 2, 2, 5, 4}}, 0, true },
{ vector<int>{{1}}, 1, false },
{ vector<int>(), 0, true },
};
int countFailed = 0;
for (int i = 0; i < ARRAYSIZE(tests); i++)
{
bool failed = false;
bool exceptionThrown = false;
int majorityElement;
try
{
majorityElement = findMajorityElement(tests[i].intArray);
if (majorityElement != tests[i].majorityElement ||
tests[i].exceptionExpected)
{
failed = true;
}
}
catch (exception)
{
exceptionThrown = true;
if (!tests[i].exceptionExpected)
{
failed = true;
}
}
if (failed)
{
countFailed++;
if (exceptionThrown)
{
cout << "Exception is thrown";
}
else
{
cout << "Majority element: " << majorityElement;
}
cout << " (Expected: ";
if (tests[i].exceptionExpected)
{
cout << "exception";
}
else
{
cout << tests[i].majorityElement;
}
cout << ")" << endl;
}
}
cout << ARRAYSIZE(tests) - countFailed << " passed, ";
cout << countFailed << " failed." << endl;
return 0;
}