# Mathematical problem mode problem in algorithm

## 169 most elements

Given an array of size n, find most of its elements. Most elements refer to elements that appear more than n / 2 in the array.

Enter a one-dimensional array and output an integer to represent the majority of elements in the array.

Input: [3,2,3]
Output: 3

Resolution:

Using hash table is the most direct method. Count the number of occurrences of each element in the array, and then find the element with the highest frequency.

```class Solution {
public:
int majorityElement(vector<int>& nums) {
unordered_map<int,int> counts;
for(const auto elem:nums){
if(counts.find(elem) != counts.end()){
counts[elem]++;
}else{
counts.insert(make_pair(elem,1));
}
}
int ans = 0;
int max = INT_MIN;
for(const auto count: counts){
if(count.second > max){
ans = count.first;
max = count.second;
}
}
return ans;
}
};
```

This problem can also be solved more concisely by Moore voting method. The basic principle of Moore voting method is: find two different elements in the array and delete them. Repeat this process until the elements in the array are the same, then the remaining elements are the main elements.

How can this method of finding the main elements be realized? In this voting method, if the same element is encountered, the number of votes will be increased by 1, and if different elements are encountered, the number of votes will be reduced by 1. When the number of votes of this element is 0, the main voting element will be replaced. When the number of main elements is greater than n / 2, it is equivalent that each main element and other elements offset each other, and at least one majority element must be left in the end.

```class Solution {
public:
int majorityElement(vector<int>& nums) {
int major = nums[0];
int count = 0;
for(int i=0;i<nums.size();++i){
if(major == nums[i]){
++count;
}else if(count == 0){
major = nums[i];
}else{
--count;
}
}
return major;
}
};
```

## 229 find mode II

Given an integer array of size n, find all elements that appear more than n / 3 times.

Input a one-dimensional array and output a one-dimensional array containing all elements whose frequency exceeds n/3

Input: [1,1,1,3,3,2,2,2]
Output: [1,2]

Resolution:

This question is the advanced use of Moore's voting method. The core of Moore's voting method is to select a majority by offsetting and counting votes, which can deduce the law: if you choose at least one representative, his votes must exceed half of the votes; If at least two deputies are elected, their votes must exceed at least one-third of the votes; By analogy, if at least m delegates are selected, their votes must at least exceed 1 / (m+1) votes.

Back to this question, if the frequency exceeds n/3, there are only two at most. Pay attention to the offset of multiple main elements: (1) a is different from B, a + +, B remains the same; (2) A is different, B is the same, a is unchanged, B + +; (3) AB is different, a –, B –;.

```class Solution {
public:
vector<int> majorityElement(vector<int>& nums) {
vector<int> res;
const int len = nums.size();
int major1, major2;
int count1 = 0, count2 = 0;
for(int i = 0; i < len; i++) {
// The same elements exist
if(nums[i] == major1){
++count1;
}
else if(nums[i] == major2){
++count2;
}
// Replace primary element
else if(count1 == 0) {
major1 = nums[i];
count1 = 1;
}
else if(count2 == 0) {
major2 = nums[i];
count2 = 1;
}
// It can only be offset when it is different from both major1 and major2. If there is only one different case where the count of the other increases, don't think of letting the different one offset. It's easy to make mistakes here
else {
count1--;
count2--;
}
}
// Is the number of elements in the inspection result more than one third
count1 = 0, count2 = 0;
for(auto &num : nums) {
if(num == major1) count1++;
else if(num == major2) count2++;
}

if(count1 > len / 3) res.push_back(major1);
if(count2 > len / 3) res.push_back(major2);
return res;
}
};
```

## reference material

LeetCode 101: easily brush problems with you (C + +) Chapter 9 skillfully solve mathematical problems

Posted on Mon, 11 Oct 2021 21:24:51 -0400 by beamerrox