Scala with TreeMap O(nlogn), find a vertical line that can cross the  most ranges
First, this question is really HARD. After observation and reasoning, we can get the assumption that, with an array of (a,b) pairs, we need to find such target 
First, this question is really HARD. After observation and reasoning, we can get the assumption that, with an array of (a,b) pairs, we need to find such target sum that we dont have to change both elements as possible as we can. So, given the condition that the limit should be the max value at least, then\n\nif the target sum == (a+b), then no need to change at all \nif the target sum  >= (min(a,b) +1) and the target sum <= max(a,b)+ limit, then we can asssume the change should be at worst 1 and at best 0 (sum==a+b)\nelse, we need to change both elements \n\nThen we may reduce this problem to another one that, we need to enumerate over the candidate threasholds to get the min change. We can implement two Treemap, representing the (min(a,b) +1)  and ( max(a,b)+ limit) each, to reduce the calculation to log(n) for each iteration. \n\nThe candidate target sum that we need to iterate over conclude, \n1. all currently existed sums of each pair \n2. min(a,b) +1\n3. max(a,b)+ limit\n\n\n\n```Scala\n\n  def minMoves(nums: Array[Int], limit: Int): Int = {\n\n    val len = nums.length\n    import java.util.{TreeMap, HashMap}\n    lazy val rightMap, leftMap = new TreeMap[Int, Int]\n    lazy val map = new HashMap[Int, Int]()\n    import scala.jdk.CollectionConverters._\n    Range(0, len / 2).foreach(i => {\n      map.put(nums(i) + nums(len - i - 1), map.getOrDefault(nums(i) + nums(len - i - 1), 0) + 1)\n      rightMap.put(Math.max(nums(i), nums(len - i - 1)) + limit, rightMap.getOrDefault(Math.max(nums(i), nums(len - i - 1)) + limit, 0) + 1)\n      leftMap.put(Math.min(nums(i), nums(len - i - 1)) + 1, leftMap.getOrDefault(Math.min(nums(i), nums(len - i - 1)) + 1, 0) + 1)\n      if (!map.containsKey(Math.min(nums(i), nums(len - i - 1) + 1))) map.put(Math.min(nums(i), nums(len - i - 1) + 1), 0)\n      if (!map.containsKey(Math.max(nums(i), nums(len - i - 1) + limit))) map.put(Math.max(nums(i), nums(len - i - 1) + limit), 0)\n    })\n\n    if (map.size == 1) return 0\n    var preSum = 0\n    val rks = rightMap.keySet()\n    rks.forEach(k =>\n      rightMap.put(k, {\n        preSum += rightMap.get(k)\n        preSum\n      }))\n    preSum = 0\n    val lks = leftMap.descendingKeySet()\n    lks.forEach(k =>\n      leftMap.put(k, {\n        preSum += leftMap.get(k)\n        preSum\n      }))\n    \n    map.keySet.asScala.map(x => len / 2 + {\n      if (Option(rightMap.lowerKey(x)).isDefined) rightMap.get(rightMap.lowerKey(x)) else 0\n    } + {\n      if (Option(leftMap.higherKey(x)).isDefined) leftMap.get(leftMap.higherKey(x)) else 0\n    } - map.get(x)).min\n  }\n```
[]