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 Scala (2.11.7) (judge verdict: Time Limit Exceeded)

object Main {
  import scala.io.StdIn.readLine
  def input[A](change: String => A) = readLine.split(" ").map(change(_))
  def main(args: Array[String]): Unit = {
    val n = input(a=>a).head.toCharArray.map(_.toInt)
    val i = input(a=>a).map(_.toInt).foldLeft((List(), List()): (List[Int], List[Int]))((acc, c) =>
      (acc._2, acc._1 :+ c)
    )
    println(new QC(i).ans)
  }
}

class QC(input: (List[Int], List[Int])) {
  val cmap = (countMap(input._1), countMap(input._2))
  val slist = (sortList(cmap._1), sortList(cmap._2))

  def countMap(a: List[Int]) = {
    a.foldLeft(Map(): Map[Int,Int])((acc,c) => {
      acc updated (c, acc.get(c).getOrElse(0) + 1)
    })
  }
  def sortList(a: Map[Int, Int]) = {
    val sum = a.values.sum
    (a.toSeq.map(k=>(k._1, sum-k._2)).sortBy(_._2), sum)
  }
  def ans(): String = {
    val a = if(slist._1._1.head._1 == slist._2._1.head._1) {
      val k = slist._1._1.head._2 + slist._2._1.tail.headOption.getOrElse((0, slist._2._2))._2
      val l = slist._1._1.tail.headOption.getOrElse((0, slist._2._2))._2 + slist._2._1.head._2
      if(k>l){ l } else { k }
    } else {
      slist._1._1.head._2 + slist._2._1.head._2
    }
    a.toString
  }
}