C++ || Bruteforce to optimal || With Explanation
In bruteforce way we can simply sort the nums\n Then  iterate in queries and for each query simply check sum of how many numbers are <= that query \n and push t
* In bruteforce way we can simply sort the nums\n* Then  iterate in queries and for each query simply check sum of how many numbers are <= that query \n* and push the count variable  int ans.\n* **Time Complexity: O(nlog(n)) of sorting +O(n*m) two loops => O(n*m)**\n* **space Complexity:  O(1)**\n\n**Code**\n```\nclass Solution {\npublic:\n    vector<int> answerQueries(vector<int>& nums, vector<int>& queries) {\n        sort(nums.begin(),nums.end());\n        vector<int>ans(queries.size());\n        for(int i=0;i<queries.size();i++)\n        {\n            int sum=0;\n            int cnt=0;\n            for(int j=0;j<nums.size();j++)\n            {\n                if(sum+nums[j]<=queries[i])\n                {\n                    sum+=nums[j];\n                    cnt++;\n                }\n            }\n            ans[i]=cnt;\n        }\n        return ans;\n    }\n};\n```\n\n* Another approach is sort the array \n* calculate prefix sum\n* Iterate in queries and calculate the upper bound of each query in prefix array\n* if we subtract prefix.begin() from the upper bound calculated above it will give no of elements whose sum is less than or equal to that particular query\n* **Time Complexity : O((m+n)log(n))**\n* **Space Complexity : O(n)**\n\n**Code** \n```\nclass Solution {\npublic:\n    vector<int> answerQueries(vector<int>& nums, vector<int>& queries) {\n        sort(nums.begin(),nums.end());\n        int n=nums.size();\n        int m=queries.size();\n        vector<int>ans;\n        vector<int>prefix(n,0);\n        prefix[0]=nums[0];\n        for(int i=1;i<n;i++)\n        {\n            prefix[i]=prefix[i-1]+nums[i];\n        }\n        for(int i=0;i<m;i++)\n        {\n            int query=queries[i];\n            ans.push_back(upper_bound(prefix.begin(),prefix.end(),query)-prefix.begin());\n        }\n        return ans;\n    }\n};\n```
['C', 'Binary Tree']