Kotlin beats 100%
Intuition\n Describe your first thoughts on how to solve this problem. \nsort nums in order to break sort nums into 2 part. \nfirst part that every element is l
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nsort nums in order to break sort nums into 2 part. \nfirst part that every element is less than current query \nsecond part that every element is equal or more than current querry\n\nuse binary search to find the first index that element is equal or more than the querry \n\nAfter that use presum left and presum right to calculate the final answer\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```\nclass Solution {\n    fun minOperations(nums: IntArray, queries: IntArray): List<Long> {\n        nums.sort()\n        val preSum = LongArray(nums.size + 1)\n\n        nums.forEachIndexed { i, number ->\n            preSum[i + 1] = preSum[i] + number\n        }\n\n        val res = mutableListOf<Long>()\n        val n = nums.size\n        val maxNum = nums.maxOrNull() ?: 0\n\n        queries.forEach { query ->\n            if (query > maxNum) {\n                res.add(query.toLong() * n - preSum.last())\n            } else {\n                val index = binarySearch(nums, query)\n                val leftSum = preSum[index]\n                val rightSum = preSum.last() - preSum[index]\n                val leftOps = query.toLong() * index - leftSum\n                val rightOps = rightSum - query.toLong() * (n - index)\n                res.add(leftOps + rightOps)\n            }\n        }\n\n        return res\n    }\n\n    private fun binarySearch(nums: IntArray, target: Int): Int {\n        var left = 0\n        var right = nums.size\n\n        while (left < right) {\n            val mid = left + (right - left) / 2\n            if (nums[mid] < target) {\n                left = mid + 1\n            } else {\n                right = mid\n            }\n        }\n\n        return left\n    }\n}\n\n```
['Binary Search', 'Sorting', 'Prefix Sum', 'Kotlin']