Beats 100% time and memory
IntuitionApproachSimple question, too bad we need to waste so much time on edge casesComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->
Simple question, too bad we need to waste so much time on edge cases

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```scala []
import scala.collection.mutable


object Solution {
    def solveQueries(nums: Array[Int], queries: Array[Int]): List[Int] = {
    if (nums.length > 2) {
      val first = nums(0)
      var second = 0
      while (second < nums.length && nums(second) == first) second += 1
      if (second == nums.length) return List.fill(queries.length)(1)
    }
    val map = mutable.HashMap[Int, List[Int]]()
    for (i <- nums.indices) {
      map.put(nums(i), map.getOrElse(nums(i), List[Int]())  :+ i)
    }

    val res: Array[Int] = Array.ofDim[Int](queries.length)
    for (i: Int <- queries.indices) {
      val n = nums(queries(i))
      if (map(n).length == 1) {
        res(i) = -1
      } else if (map(n).length == 2) {
        res(i) = Math.min(map(n)(1) - map(n).head, map(n).head + nums.length - map(n)(1))
      } else {
        val a: Int = map(n).search(queries(i)).insertionPoint
        val left = if (a == 0) map(n).last else (map(n)(a - 1))
        val right = if (a == map(n).length - 1) map(n).head else map(n)(a + 1)
        res(i) = Math.min((queries(i) + nums.length - left) % nums.length, (right + nums.length - queries(i)) % nums.length)
      }
    }
    res.toList
  }
}

```
['Scala']