Problem:
Score : 300 points

Problem Statement

A sequence a_1,a_2,... ,a_n is said to be /\/\/\/ when the following conditions are satisfied:

For each i = 1,2,..., n-2, a_i = a_{i+2}.

Exactly two different numbers appear in the sequence.

You are given a sequence v_1,v_2,...,v_n whose length is even.
We would like to make this sequence /\/\/\/ by replacing some of its elements.
Find the minimum number of elements that needs to be replaced.

Constraints

2 \leq n \leq 10^5

n is even.

1 \leq v_i \leq 10^5

v_i is an integer.

Input

Input is given from Standard Input in the following format:

n
v_1 v_2 ... v_n

Output

Print the minimum number of elements that needs to be replaced.

Sample Input 1

4
3 1 3 2

Sample Output 1

1

The sequence 3,1,3,2 is not /\/\/\/, but we can make it /\/\/\/ by replacing one of its elements: for example, replace the fourth element to make it 3,1,3,1.

Sample Input 2

6
105 119 105 119 105 119

Sample Output 2

0

The sequence 105,119,105,119,105,119 is /\/\/\/.

Sample Input 3

4
1 1 1 1

Sample Output 3

2

The elements of the sequence 1,1,1,1 are all the same, so it is not /\/\/\/.

Solution in Rust (1.42.0) (judge verdict: Accepted)

use std::collections::HashMap;
use itertools::Itertools;

#[macro_use]
macro_rules! error {
    ($($arg:tt)+) => (
        println!($($arg)+);
    )
}

fn main() {
    let mut input_buffer = String::new();
    let stdin = std::io::stdin();
    if let Err(err) = stdin.read_line(&mut input_buffer) {
        error!("Cannot read a line from stdin: {}", err);
        return;
    }
    let input_buffer = input_buffer.trim();
    
    let sequence_length: usize = match input_buffer.parse() {
        Ok(n) => n,
        Err(err) => {
            error!("\"{}\" is not a positive integer: {}", input_buffer, err);
            return;
        },
    };

    if sequence_length == 0 {
        error!("The length has to be a positive (non-zero) even integer. ");
        return;
    }
    if sequence_length % 2 != 0 {
        error!("The length of sequence has to be even. ");
        return;
    }
    
    let mut input_buffer = String::new();
    if let Err(err) = stdin.read_line(&mut input_buffer) {
        error!("Cannot read a line from stdin: {}", err);
        return;
    }
    let input_buffer = input_buffer.trim();
    
    let sequence = input_buffer
        .split(' ')
        .map(|s| s.parse::<i32>())
        .collect::<Result<Vec<_>, _>>();
    let sequence = match sequence {
        Ok(s) => s, 
        Err(err) => {
            error!("\"{}\" are not integers: {}", input_buffer, err);
            return;
        }, 
    };

    if sequence.len() != sequence_length {
        error!("The length of sequence is different from reserved one: {} reserved, {} used. ", sequence_length, sequence.len());
        return;
    }

    let mut even_items: HashMap<i32, i32> = HashMap::new();
    let mut odd_items: HashMap<i32, i32> = HashMap::new();
    for pair in sequence.chunks(2) {
        *even_items.entry(pair[0]).or_insert(0) += 1;
        *odd_items.entry(pair[1]).or_insert(0) += 1;
    }
    let even_items = even_items;
    let odd_items = odd_items;

    let even_items_sorted: Vec<(i32, i32)> = even_items.into_iter().sorted_by(|a, b| a.1.cmp(&b.1)).rev().collect();
    let odd_items_sorted: Vec<(i32, i32)> = odd_items.into_iter().sorted_by(|a, b| a.1.cmp(&b.1)).rev().collect();

    let even_max = even_items_sorted[0];
    let odd_max = odd_items_sorted[0];
    if even_max.0 != odd_max.0 {
        println!("{}", sequence_length as i32 - (even_max.1 + odd_max.1));
    }
    else {
        let odd_max2 = *odd_items_sorted.get(1).unwrap_or(&(even_max.0 + 1, 0i32));
        let even_max2 = *even_items_sorted.get(1).unwrap_or(&(odd_max.0 + 1, 0i32));
        println!("{}", sequence_length as i32 - std::cmp::max(even_max2.1 + odd_max.1, even_max.1 + odd_max2.1));
    }
}