Java | Sort | Fully Explained
Intuition\nFor every query q just remove the biggest elements from the total sum until the condition sum <= q is satisfied.\n\nThis works because we simply remo
# Intuition\nFor every query `q` just remove the biggest elements from the total sum until the condition `sum <= q` is satisfied.\n\nThis works because we simply remove elements in arbitrary indexes, and the invariant of the other elements being subsequence of the array is kept because the order is never changed.\n\nTo remove the biggest elements one at a time we need to sort or use a heap.\n\n# Code\n```\nclass Solution {\n    public int[] answerQueries(int[] nums, int[] queries) {\n        int n = nums.length;\n        int m = queries.length;\n        int[] answer = new int[m];\n\n        Arrays.sort(nums);\n        int sum = Arrays.stream(nums).sum();\n\n        for (int i = 0; i < m; i++) {\n            int j = n-1;\n            int curSum = sum;\n            while (j >= 0 && curSum > queries[i]) {\n                curSum -= nums[j];\n                j--;\n            }\n            answer[i] = j + 1;\n        }\n\n        return answer;\n        \n    }\n}\n```
['Java']