✅ C++ || 2 Pointer || Easy || Simple & Short || Algorithm
881. Boats to Save People\nKNOCKCAT\n\n\n1. Easy C++\n2. Line by Line Explanation with Comments.\n3. Detailed Explanation \u2705\n4. Inituton is to use two poin
# 881. Boats to Save People\n**KNOCKCAT**\n\n```\n1. Easy C++\n2. Line by Line Explanation with Comments.\n3. Detailed Explanation \u2705\n4. Inituton is to use two pointer approach,\n   to make pair of lightest & heaviest person by sorting vector first.\n5. Please Upvote if it helps\u2B06\uFE0F\n6. Link to my Github Profile contains a repository of Leetcode with all my Solutions. \u2B07\uFE0F\n\t// \uD83D\uDE09If you Like the repository don\'t foget to star & fork the repository\uD83D\uDE09\n```\n``` ```\n[LeetCode](http://github.com/knockcat/Leetcode) **LINK TO LEETCODE REPOSITORY**\n``` ```\n\nPlease upvote my comment so that i get to win the 2022 giveaway and motivate to make such discussion post.\n**Happy new Year 2023 to all of you**\n**keep solving keep improving**\nLink To comment\n[Leetcode Give away comment](https://leetcode.com/discuss/general-discussion/2946993/2022-Annual-Badge-and-the-Giveaway/1734919)\n\n\n**ALGORITHM**\n* **Sort the Vector.**\n* We can make the **lightest & heaviest person to go together.**\n* using **Two pointer appraoch** **i is the lightest person** & j **is the heaviest.**\n* Traversing t**ill start <= end**.\n\t* if **sum of lightest & heaviest person are under limit** then **both can go together** , **updating i & j both**.\n\t* **If sum exceeds the limit**, then **heaviest person will go alone**, hence **updating only  the j pointer.**\n\t* **increment the count .**\n* **Return cnt.**\n\nANALYSIS:-\n* **TIME COMPLEXITY** :- O(NlogN)\n* **SPACE COMPLEXITY** :-  O(1)\n\n``` ```\n**CODE WITH EXPLANATION**\n```\n\t\t\t\t// \uD83D\uDE09\uD83D\uDE09\uD83D\uDE09\uD83D\uDE09Please upvote if it helps \uD83D\uDE09\uD83D\uDE09\uD83D\uDE09\uD83D\uDE09\nclass Solution {\npublic:\n    int numRescueBoats(vector<int>& people, int limit) {\n       \n        // sort vector\n        sort(people.begin(),people.end());\n        \n        int i = 0, j = people.size() - 1,cnt = 0;\n        \n        while(i <= j)\n        {   \n            // lightest person + heaviest person sum <= limit\n            // they can go together\n            if(people[i] + people[j] <= limit)\n            {\n                ++i;\n                --j;\n            }\n            // if sum is over the limit,\n            // heaviest will go alone.\n            else\n                --j;\n            \n            ++cnt;  // number of boats\n        }\n        \n        return cnt;\n        \n    }\n\t// for github repository link go to my profile.\n};\n```
['Two Pointers', 'C', 'Sorting', 'C++']