c++ || easy solution with explanation|| solution
Intuition\n Describe your first thoughts on how to solve this problem. \nHow to solve think rationally .....\n\nthe first step of thinking approach is the brute
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nHow to solve think rationally .....\n\nthe first step of thinking approach is the brute force method ..\nSo what the question ask us for ...\nto return all the number of elements whose sum is smaller or equal to the query indexed element ...\nthe question itself has all the hidden hints if you find them this question can be solve in its first attempt only \n\nthink the maximum elements can be found if the array is in sorted order to add the elements in the list of subarray if not equal to the query element \n\nthink as if add the smaller elements to the list to make the number of element max that\'s why step 1 is sort then at each element we have to find again and again sum to keep a track form a array of sum which stores all the sum of element till that element and move forward\n\nwe can traverse for each element in the prefix element in time n and se whether it follows the problem or not. \n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nstep 1: sort the nums array\nstep 2: find the prefix sum of the element and store in the array \nstep 3: traverse through each element in the array ogf query elements and check the index in the presum which is greater than the element it self and add that element into the answer array.\nstep 4: return the answer vector.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\ntime for sorting $$o(nlogn)$$ \ntime for prefix sum n\ntime to seach the index is log n and for m elements it is mlogn\nso max time taken is $$O(NlogN)$$\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nprefix sum array n\nanswer array m\nspace complexity $$O(N)$$ex\n\n# Code\n```\nclass Solution {\npublic:\n    vector<int> answerQueries(vector<int>& nums, vector<int>& queries) {\n        //sorting the elements to find the correct order to perform \n        sort(nums.begin(),nums.end());\n        int n=nums.size();\n        int m=queries.size();\n\n        vector<int> presum;\n        int sum=0;\n        //finding the prefix sum of the input vector\n        for(int i=0;i<n;i++)\n        {\n            sum+=nums[i];\n            presum.push_back(sum);\n\n        }\n        //traverse through the query elements and find the index below them using binary search\n        vector<int>ans;\n        for(int i=0;i<m;i++)\n        {\n            // binary search for each element\n            int s=0,e=n-1,mid=s+(e-s)/2;\n            int idx=0;\n            while(s<=e)\n            {\n                if(presum[mid]<=queries[i])\n                {\n                    s=mid+1;\n                    idx=mid+1;\n                }\n                else{\n                    e=mid-1;\n                }\n                mid=s+(e-s)/2;\n            }\n            ans.push_back(idx);\n        }\n\n        //return the answer\n        return ans;\n    }\n};\n```
['Binary Search', 'Greedy', 'Sorting', 'Prefix Sum', 'C++']