Skip to content

Instantly share code, notes, and snippets.

@wx257osn2
Forked from qnighy/majority.rs
Last active July 15, 2024 22:23
Show Gist options
  • Save wx257osn2/8132166217a3484b1fe3ec7c19d3e0cd to your computer and use it in GitHub Desktop.
Save wx257osn2/8132166217a3484b1fe3ec7c19d3e0cd to your computer and use it in GitHub Desktop.
Fast 2-pass majority finder
// Copyright 2024 I
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// Porting to C++ by I, Licensed by MIT like original one. Original license notice is below:
// Copyright 2024 Masaki Hara
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include<ranges>
#include<optional>
/// Returns the majority element if it exists and std::nullopt otherwise.
///
/// The majority element is the element that appears more than n / 2 times in the input range.
///
/// Assumption:
///
/// - `r` is finite.
/// - `r`'s first and second responses contain the same multi-set of elements.
/// - `operator==` for `std::ranges::range_value_r<R>` is pure and is indeed an equivalence relation.
template<std::ranges::forward_range R>
static std::optional<std::ranges::range_value_t<R>> majority(const R& r){
if(std::ranges::empty(r))
return std::nullopt;
const auto candidate = [&r](){
auto it = std::ranges::cbegin(r);
auto candidate = *it++;
// Invariant: for all possible element value x,
// for x == candidate, 2 * subarray.count(x) - subarray.len() <= count
// for x != candidate, 2 * subarray.count(x) - subarray.len() <= -count
// Also, count is nonnegative.
std::size_t count = 1;
for(const auto end = std::ranges::cend(r); it != end; ++it){
if(*it == candidate)
++count;
else if(count == 0){
candidate = *it;
count = 1;
}
else
--count;
}
// The majority, if any, is set to this variable,
// because otherwise it would violate the postcondition
// derived from the invariant's second inequality.
return candidate;
}();
// Check if the candidate is indeed the majority.
// This is sometimes not the case, e.g. for the input [1, 2, 3] (where 3 is the candidate).
if constexpr(std::ranges::sized_range<R>){
std::size_t count = 0;
const auto total = std::ranges::size(r);
for(const auto& v : r){
if(v == candidate)
++count;
if(count > total/2)
return candidate;
}
return std::nullopt;
}
else{
std::size_t total = 0, count = 0;
for(const auto& v : r){
++total;
if(v == candidate)
++count;
}
if(count > total/2)
return candidate;
return std::nullopt;
}
}
#include<cassert>
#include<vector>
int main() {
assert(majority(std::vector<int>{}) == std::nullopt);
assert(majority(std::vector{1}) == 1);
assert(majority(std::vector{1, 1}) == 1);
assert(majority(std::vector{1, 2}) == std::nullopt);
assert(majority(std::vector{1, 1, 1}) == 1);
assert(majority(std::vector{1, 1, 2}) == 1);
assert(majority(std::vector{1, 2, 1}) == 1);
assert(majority(std::vector{2, 1, 1}) == 1);
assert(majority(std::vector{1, 2, 3}) == std::nullopt);
assert(majority(std::vector{1, 1, 1, 1}) == 1);
assert(majority(std::vector{1, 1, 1, 2}) == 1);
assert(majority(std::vector{1, 1, 2, 1}) == 1);
assert(majority(std::vector{1, 2, 1, 1}) == 1);
assert(majority(std::vector{2, 1, 1, 1}) == 1);
assert(majority(std::vector{1, 1, 2, 2}) == std::nullopt);
assert(majority(std::vector{1, 2, 1, 2}) == std::nullopt);
assert(majority(std::vector{2, 1, 1, 2}) == std::nullopt);
assert(majority(std::vector{1, 1, 2, 3}) == std::nullopt);
assert(majority(std::vector{1, 2, 1, 3}) == std::nullopt);
assert(majority(std::vector{2, 1, 1, 3}) == std::nullopt);
assert(majority(std::vector{1, 2, 3, 1}) == std::nullopt);
assert(majority(std::vector{2, 1, 3, 1}) == std::nullopt);
assert(majority(std::vector{2, 3, 1, 1}) == std::nullopt);
assert(majority(std::vector{1, 2, 3, 4}) == std::nullopt);
assert(majority(std::vector{1, 1, 1, 1, 1}) == 1);
assert(majority(std::vector{1, 1, 1, 1, 2}) == 1);
assert(majority(std::vector{1, 1, 1, 2, 1}) == 1);
assert(majority(std::vector{1, 1, 2, 1, 1}) == 1);
assert(majority(std::vector{1, 2, 1, 1, 1}) == 1);
assert(majority(std::vector{2, 1, 1, 1, 1}) == 1);
assert(majority(std::vector{1, 1, 1, 2, 2}) == 1);
assert(majority(std::vector{1, 1, 2, 1, 2}) == 1);
assert(majority(std::vector{1, 2, 1, 1, 2}) == 1);
assert(majority(std::vector{2, 1, 1, 1, 2}) == 1);
assert(majority(std::vector{1, 1, 2, 2, 1}) == 1);
assert(majority(std::vector{1, 2, 1, 2, 1}) == 1);
assert(majority(std::vector{2, 1, 1, 2, 1}) == 1);
assert(majority(std::vector{1, 2, 2, 1, 1}) == 1);
assert(majority(std::vector{2, 1, 2, 1, 1}) == 1);
assert(majority(std::vector{2, 2, 1, 1, 1}) == 1);
assert(majority(std::vector{1, 1, 1, 2, 3}) == 1);
assert(majority(std::vector{1, 1, 2, 1, 3}) == 1);
assert(majority(std::vector{1, 2, 1, 1, 3}) == 1);
assert(majority(std::vector{2, 1, 1, 1, 3}) == 1);
assert(majority(std::vector{1, 1, 2, 3, 1}) == 1);
assert(majority(std::vector{1, 2, 1, 3, 1}) == 1);
assert(majority(std::vector{2, 1, 1, 3, 1}) == 1);
assert(majority(std::vector{1, 2, 3, 1, 1}) == 1);
assert(majority(std::vector{2, 1, 3, 1, 1}) == 1);
assert(majority(std::vector{2, 3, 1, 1, 1}) == 1);
assert(majority(std::vector{1, 1, 2, 2, 3}) == std::nullopt);
assert(majority(std::vector{1, 2, 1, 2, 3}) == std::nullopt);
assert(majority(std::vector{2, 1, 1, 2, 3}) == std::nullopt);
assert(majority(std::vector{1, 1, 2, 3, 2}) == std::nullopt);
assert(majority(std::vector{1, 2, 1, 3, 2}) == std::nullopt);
assert(majority(std::vector{2, 1, 1, 3, 2}) == std::nullopt);
assert(majority(std::vector{1, 1, 3, 2, 2}) == std::nullopt);
assert(majority(std::vector{1, 2, 3, 1, 2}) == std::nullopt);
assert(majority(std::vector{2, 1, 3, 1, 2}) == std::nullopt);
assert(majority(std::vector{1, 3, 1, 2, 2}) == std::nullopt);
assert(majority(std::vector{1, 3, 2, 1, 2}) == std::nullopt);
assert(majority(std::vector{2, 3, 1, 1, 2}) == std::nullopt);
assert(majority(std::vector{3, 1, 1, 2, 2}) == std::nullopt);
assert(majority(std::vector{3, 1, 2, 1, 2}) == std::nullopt);
assert(majority(std::vector{3, 2, 1, 1, 2}) == std::nullopt);
assert(majority(std::vector{1, 1, 2, 3, 4}) == std::nullopt);
assert(majority(std::vector{1, 2, 1, 3, 4}) == std::nullopt);
assert(majority(std::vector{1, 2, 3, 1, 4}) == std::nullopt);
assert(majority(std::vector{1, 2, 3, 4, 1}) == std::nullopt);
assert(majority(std::vector{2, 1, 1, 3, 4}) == std::nullopt);
assert(majority(std::vector{2, 1, 3, 1, 4}) == std::nullopt);
assert(majority(std::vector{2, 1, 3, 4, 1}) == std::nullopt);
assert(majority(std::vector{2, 3, 1, 1, 4}) == std::nullopt);
assert(majority(std::vector{2, 3, 1, 4, 1}) == std::nullopt);
assert(majority(std::vector{2, 3, 4, 1, 1}) == std::nullopt);
assert(majority(std::vector{1, 2, 3, 4, 5}) == std::nullopt);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment