Two Different C++ Approaches | With and Without Prefix Sum
Please do upvote and share.\n\n\n\n1. Using Prefix Sum\ncpp\nclass Solution {\npublic:\n    vector<int> answerQueries(vector<int> &nums, vector<int> &queries) {
Please do **upvote** and **share**.\n\n<br>\n\n1. Using Prefix Sum\n```cpp\nclass Solution {\npublic:\n    vector<int> answerQueries(vector<int> &nums, vector<int> &queries) {\n        sort(nums.begin(), nums.end());\n        for (int i = 1; i < nums.size(); i++) {\n            nums[i] += nums[i - 1];\n        }\n        vector<int> ans;\n        for (int query : queries) {\n            int j = 0;\n            for (; j < nums.size(); j++) {\n                if (nums[j] > query) break;\n            }\n            ans.emplace_back(j);\n        }\n        return ans;\n    }\n};\n```\n<br>\n\n2. Without using Prefix Sum\n```cpp\nclass Solution {\npublic:\n    vector<int> answerQueries(vector<int> &nums, vector<int> &queries) {\n        sort(nums.begin(), nums.end());\n        vector<int> res;\n        for (int i = 0; i < queries.size(); i++) {\n            int sum = 0, sz = 0;\n            for (int j = 0; j < nums.size(); j++) {\n                if (sum + nums[j] <= queries[i]) {\n                    sz++;\n                    sum += nums[j];\n                } else {\n                    break;\n                }\n            }\n            res.push_back(sz);\n        }\n        return res;\n    }\n};\n```
['C', 'Sorting', 'Prefix Sum', 'C++']