best kotlin solution-100%
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n\nclass MajorityChecker(arr: IntArray) {\n    private val frequencies: HashMap<Int, ArrayList<Int>> = HashMap()\n\n    init {\n        for (i in arr.indices) {\n            frequencies.computeIfAbsent(arr[i]) { ArrayList() }.add(i)\n        }\n    }\n\n    fun query(left: Int, right: Int, threshold: Int): Int {\n        for ((num, indices) in frequencies) {\n            if (indices.size >= threshold) {\n                val start = lowerBound(indices, left)\n                val end = upperBound(indices, right)\n                if (end - start >= threshold) {\n                    return num\n                }\n            }\n        }\n        return -1\n    }\n\n    private fun lowerBound(indices: ArrayList<Int>, target: Int): Int {\n        var left = 0\n        var right = indices.size\n        while (left < right) {\n            val mid = left + (right - left) / 2\n            if (indices[mid] < target) {\n                left = mid + 1\n            } else {\n                right = mid\n            }\n        }\n        return left\n    }\n\n    private fun upperBound(indices: ArrayList<Int>, target: Int): Int {\n        var left = 0\n        var right = indices.size\n        while (left < right) {\n            val mid = left + (right - left) / 2\n            if (indices[mid] <= target) {\n                left = mid + 1\n            } else {\n                right = mid\n            }\n        }\n        return left\n    }\n}\n```
['Kotlin']