Kotlin BinarySearch
\nimport java.util.*\n\nclass MajorityChecker(arr: IntArray) {\n    val map2: Map<Int, ArrayList<Int>>\n\n    init {\n        map2 = HashMap<Int, ArrayList<Int>
```\nimport java.util.*\n\nclass MajorityChecker(arr: IntArray) {\n    val map2: Map<Int, ArrayList<Int>>\n\n    init {\n        map2 = HashMap<Int, ArrayList<Int>>()\n        for (i in arr.indices) {\n            map2.putIfAbsent(arr[i], ArrayList())\n            map2[arr[i]]!!.add(i)\n        }\n    }\n\n    fun query(left: Int, right: Int, threshold: Int): Int {\n        var total = 0\n        for ((key) in map2) {\n            val list = map2[key]\n            val L = searchL(list!!, left)\n            val R = searchR(list!!, right)\n            if (L <= R && L >= 0 && R < list!!.size) {\n                if (R - L + 1 >= threshold) {\n                    return key\n                }\n                total += R - L + 1\n                if (right - left + 1 - total < threshold) break\n            }\n        }\n        return -1\n    }\n\n    fun searchR(list: ArrayList<Int>, right: Int): Int {\n        var l = 0\n        var r = list.size - 1\n        while (l <= r) {\n            val mid = l + (r - l) / 2\n            if (list[mid] == right) {\n                return mid\n            } else if (list[mid] < right) {\n                l = mid + 1\n            } else {\n                r = mid - 1\n            }\n        }\n        return r\n    }\n\n    fun searchL(list: ArrayList<Int>, left: Int): Int {\n        var l = 0\n        var r = list.size - 1\n        while (l <= r) {\n            val mid = l + (r - l) / 2\n            if (list[mid] == left) {\n                return mid\n            } else if (list[mid] < left) {\n                l = mid + 1\n            } else {\n                r = mid - 1\n            }\n        }\n        return l\n    }\n}\n```
[]