✅ Java with Explanation | Easy Sorting | Complexity Analysis |
Sequence of nums array doesn\'t matter. So we can sort it to check for this step:\n\nif(nums[j]<=queries[i])\n\nand update the particular query simultaneously.\
**Sequence of ```nums``` array doesn\'t matter. So we can sort it to check for this step:**\n```\nif(nums[j]<=queries[i])\n```\n**and update the particular query simultaneously.**\n```\n\nclass Solution {\n    public int[] answerQueries(int[] nums, int[] queries) {\n        Arrays.sort(nums);\n        int[] ans = new int[queries.length];\n        for(int i=0;i<queries.length;i++){\n            int size=0;\n            for(int j=0;j<nums.length;j++){\n                if(nums[j]<=queries[i]){\n                    queries[i]-=nums[j];\n                    size++;\n                }\n                else break;\n            }\n            ans[i] = size;\n        }\n        return ans;\n    }\n}\n```\n\n### Complexity Analysis\n\n**Time Complexity: *O(nm)***\n    For every query, we are traversing the nums array of length n in the worst case.\n\tAnd length of query array is m.\n\tAlso, sorting takes **O(log n)**, but it the other part would be dominant.\n\t   Hence, TC: *O(nm)*\n\t\n**Space Complexity: *O(1)***\n   NO EXTRA SPACE REQUIRED
['Sorting', 'Java']