Skip to content

Commit

Permalink
Boyer–Moore majority vote algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
y4n9b0 committed May 13, 2024
1 parent 8ef39a7 commit b0756a1
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions _posts/2024-05-13-Boyer–Moore-majority-vote-algorithm.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
---
layout: post
title: Boyer–Moore majority vote algorithm
date: 2024-05-13 13:30:00 +0800
categories: algorithm
tags: vote
published: true
---

* content
{:toc}

[Boyer–Moore majority vote algorithm](https://www.cs.utexas.edu/users/moore/best-ideas/mjrty/index.html){:target="_blank"}

## problem

[Kotlin Heroes: Practice 10 - Problem A](https://codeforces.com/contest/1959/problem/A){:target="_blank"}

You are given an array 𝑎 consisting of 𝑛(𝑛≥3) positive integers.
It is known that in this array, all the numbers except one are the same
(for example, in the array [4,11,4,4] all numbers except one are equal to 4).

Print the index of the element that does not equal others. The numbers in the array are numbered from one.

**Input**<br>
The first line contains a single integer 𝑡(1≤𝑡≤100). Then 𝑡 test cases follow.<br>
The first line of each test case contains a single integer 𝑛(3≤𝑛≤100) — the length of the array 𝑎.<br>
The second line of each test case contains 𝑛 integers 𝑎1,𝑎2,…,𝑎𝑛(1≤𝑎𝑖≤100).<br>
It is guaranteed that all the numbers except one in the 𝑎 array are the same.

**Output**<br>
For each test case, output a single integer — the index of the element that is not equal to others.

**Example**
```
input
4
4
11 13 11 11
5
1 4 4 4 4
10
3 3 3 3 10 3 3 3 3 3
3
20 20 10
output
2
1
5
3
```

## solution

```kotlin
fun main() {
repeat(readln().toInt()) { println(solve()) }
}

fun solve(): Int {
readln()
val array = readln().split(' ').map { it.toInt() }.toIntArray()
val list = mutableListOf<Int>()
repeat(3) {
if (list.isEmpty() || list[0] == array[it]) {
list.add(array[it])
} else {
list.removeAt(0)
}
}
val majority = list[0]
return array.indexOfFirst { it != majority } + 1
}
```

0 comments on commit b0756a1

Please sign in to comment.