✅C++|Binary_search|Upper_bound|Brute Force| All Approaches🔥
Intuition\n Describe your first thoughts on how to solve this problem. \nHint: Use prefix sum that can give already computed sum upto some index and that is wha
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nHint: Use prefix sum that can give already computed sum upto some index and that is what we need but first sort the array that will maximize the subarray length .\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:O(nlogn)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n**Brute Force**\n```\nclass Solution {\npublic:\n    vector<int> answerQueries(vector<int>& arr, vector<int>& query) {\n        // incase of subsequnce Always remeber you can sort no need of order\n        sort(arr.begin(),arr.end());\n        vector<int>v;\n        for(auto q:query){\n            int sum=0,c=0;\n            for(int i=0;i<arr.size();i++){\n                sum+=arr[i];\n                if(sum<=q)c++;\n            }\n            v.push_back(c);\n        }\n        return v;\n}\n};\n```\n**Using Binary Search**\n```\nclass Solution {\npublic:\n    vector<int> answerQueries(vector<int>& arr, vector<int>& query) {\n        // incase of subsequnce Always remeber you can sort no need of order\n        sort(arr.begin(),arr.end());\n        int n=arr.size();\n        for(int i=1;i<n;i++)arr[i]+=arr[i-1];\n        vector<int>v;\n        for(auto q:query){\n            int l=0,h=n-1,ans=0;\n           while(l<=h){\n               int mid=l+(h-l)/2;\n               if(arr[mid]<=q){\n                   ans=mid+1;\n                   l=mid+1;\n               }\n               else h=mid-1;\n           }\n            v.push_back(ans);\n        }\n        return v;\n}\n};\n```\n**Using Upper_bound**\n```\nclass Solution {\npublic:\n    vector<int> answerQueries(vector<int>& arr, vector<int>& query) {\n        // incase of subsequnce Always remeber you can sort no need of order\n        sort(arr.begin(),arr.end());\n        int n=arr.size();\n        for(int i=1;i<n;i++)arr[i]+=arr[i-1];\n        vector<int>v;\n        for(auto q:query){\n            int sum=0,c=0;\n            int l=0,h=n-1,ans=0;\n            v.push_back(upper_bound(arr.begin(),arr.end(),q)-arr.begin());\n        }\n        return v;\n}\n};\n```
['C++']