C++ 🔥 2 pointer 🔥 Easy greedy approach 🔥 Simple sort
PLEASE UPVOTE\uD83D\uDC4D\n# Approach\nOnly 2 persons maximum allowed on one boat so it can be solved by 2 pointers and find sum of maximum and minimum element.
# **PLEASE UPVOTE\uD83D\uDC4D**\n# Approach\nOnly 2 persons maximum allowed on one boat so it can be solved by 2 pointers and find sum of maximum and minimum element.\nif sum is less than or equal to limit then we can group them in one boat otherwise only person with max weight is allowed.\nUsing this approach we can find minimum number of boats.\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```\nclass Solution {\npublic:\n    int numRescueBoats(vector<int>& a, int k) {\n        int n = a.size();\n        int cnt = 0, sum = 0, ct = 0;\n        sort(a.begin(),a.end());\n\n        int i = 0, j = n-1;\n        while(j >= 0 && i <= j)\n        {\n            if(i == j)\n            {\n                ct++;\n                i++;\n            }\n            else if(a[i]+a[j] <= k)\n            {\n                i++;\n                j--;\n                ct++;\n            }\n            else\n            {\n                j--;\n                ct++;\n            }\n        }\n\n        return ct;\n    }\n};\n```\n\n![e2515d84-99cf-4499-80fb-fe458e1bbae2_1678932606.8004954.png](https://assets.leetcode.com/users/images/8bf057a5-2543-4119-85b6-9c7543357354_1680482249.412516.png)\n
['Two Pointers', 'Greedy', 'Sorting', 'C++']