✅✅ C++ | Easy Solution | Sorting & Binary Search | Clean Code
This question is based on binary search.\n\nApproach:\n1. First sort the items vector.\n2. Now store the maximum value of beauty till the current index i ( i >=
This question is based on **binary search**.\n\n**Approach:**\n1. First **sort** the items vector.\n2. Now store the maximum value of beauty till the current index i ( i >=0 && i <= n-1)\n3. Now iterate on the queries vector and **find the greatest index of the q[i] in the items vector using binary search.**\n4. **If the index is -1** then there is no such value in items vector. Simply put 0 in ans[i]. \n5. Store the value corresponding to this index in ans[i].\n\n**Code:**\n\n\n    int binary_s(vector<vector<int>>&items, int val){\n        \n        int lo = 0;\n        int hi = items.size() - 1;\n        \n        int ans = -1;\n        while(lo <= hi){\n            int mid = lo + (hi - lo)/2;\n            if(items[mid][0] <= val){\n                ans = mid;\n                lo = mid + 1;\n            }\n            else{\n                hi = mid - 1;\n            }\n        }\n        \n        return ans;\n    }\n    \n    vector<int> maximumBeauty(vector<vector<int>>& items, vector<int>& q) {\n        \n        int n = items.size();\n        sort(items.begin() , items.end());\n        \n        vector<int>a(n);\n        int max_val = 0;\n        for(int i = 0 ; i < n ; i++){\n            int val = items[i][1];\n            max_val = max(max_val , val);\n            a[i] = max_val;\n        }\n        \n        int m = q.size();\n        \n        vector<int>ans(m);\n        for(int i = 0 ; i < m ; i++){\n            int idx = binary_s(items, q[i]);\n            if(idx == -1){\n                ans[i] = 0;\n            }\n            else{\n                ans[i] = a[idx];   \n            }\n        }\n        \n        return ans;\n    }\n\t\n\tPlease upvote the post if this helped you!
['C', 'Binary Tree']