[C++] 2 Approaching : Sorting + Brute-force   | | PrefixSum + BinarySearch
\n\n# Sorting + Brute-force\n\nclass Solution {\npublic:\n    vector<int> answerQueries(vector<int>& nums, vector<int>& queries) {\n        sort(nums.begin(),nu
\n\n# Sorting + Brute-force\n```\nclass Solution {\npublic:\n    vector<int> answerQueries(vector<int>& nums, vector<int>& queries) {\n        sort(nums.begin(),nums.end());\n        vector<int>ans;\n        for(auto x:queries){\n            int sum=0;\n            int MAX=0;\n            for(int i=0;i<nums.size();i++){\n                sum+=nums[i];\n                if(sum<=x){\n                    MAX=max(MAX,i+1);\n                }\n            }\n            ans.push_back(MAX);\n        }\n        return ans;\n    }\n};\n```\n# PrefixSum + BinarySearch\n```\nclass Solution {\npublic:\n    vector<int> answerQueries(vector<int>& nums, vector<int>& queries) {\n        sort(nums.begin(),nums.end());\n        vector<int>ans;\n        vector<int>prefix(nums.size());\n        prefix[0]=nums[0];\n        for(int i=1;i<nums.size();i++){\n            prefix[i]=prefix[i-1]+nums[i];\n        }\n        for(auto x:queries){\n            auto it=lower_bound(prefix.begin(),prefix.end(),x);\n            if(it==prefix.end() || *it!=x ) ans.push_back(it-prefix.begin());\n            else ans.push_back(it-prefix.begin()+1);\n        }\n        return ans;\n    }\n};\n```
['C++']