[Java/C++] A very Easy EXPLANATION || TRUST ME <^^>
How\'s going Ladies - n - Gentlemen, today we are going to solve another cooleast problem i.e. Boats to Save People\n\nSo, let\'s see what problem is saying,\n\
How\'s going Ladies - n - Gentlemen, today we are going to solve another cooleast problem i.e. **Boats to Save People**\n\nSo, let\'s see what problem is saying,\n```\nWe have given weight of peoples\nLimit weight of boat & boats are available infinite\nBut, we can only put Minimum of 1 person & Maximum of 2 person\n```\n\nLet\'s take one example, inorder to understand the problem:-\n**Input**: people = [7,9,3,2,8,6,4,5], limit = 10\n**Output**: 5\n\nOkay so let\'s see what the very **`Brute-Force approach`** comes in your mind after looking at this,\nWe have the array & use 2 pointers.\n* One of them starting from **0th index** and another from **1st index**. We gonna have one count variable as well, which helps us in get the no. of count of boats. \n\n\n* Sum them up & if their weight is less than or equals to limit, then increment the count otherwise increment the **1st index** pointer, until we find less then or equal to the limit\n* If unable to find anyone of them, we gonna increment the count of **0th index** pointer\n\n![image](https://assets.leetcode.com/users/images/b2565582-dfa0-46c7-af1a-f2bc1c59158d_1648085126.5195324.png)\n\n![image](https://assets.leetcode.com/users/images/dca29c94-c337-409c-9149-1b2b83be84e0_1648085241.2805197.png)\n\nSo, as you can see it\'s not a good way, because we are re-visiting the character which we have already visited & we are recalculating it.\n\nIn this Approach, our **Time Complexity will be O(N^2)**\n\n<hr>\n<hr>\n\n```\nNow, you\'ll ask dude, then what\'s the Optimise solution\n```\n\n<hr>\n<hr>\n\nI\'ll say okay, let\'s move to Optimise Approach.\nSo, for optimisation my **1st Step is sort the Array**\n\nNow you ask, what\'s the benefit of doing that? Well, the benefit of sorting is, we can take one max. weight person & one min weight person in the boat/ \nAnd for that we gonna sort the array.\n\nUsing above example :- \n`[7,9,3,2,8,6,4,5]`\n**After Sorting**\n`[2,3,4,5,6,7,8,9]`\n\nSo, for optimisation my **2nd Step will be create 2 pointer\'s**\n* One will be my **left pointer** intially on the extreme left i.e. starting of the array\n\n\n* One will be my **right pointer** intially on the extreme right i.e. end of the array\n\n```\nOkay, now when I sum up the value of:\nLeft + Right pointer, there could be 2 Possiblities\n\n1.0 (sum <= limit)      "sum could be less then or equals to my limit"\n2.0 (sum > limit)      "sum could be greater then my limit"\n```\n\nLet\'s talk about what we\'ll do when we face **`possibility no. 1.0`**\nIf that\'s the case then we only require single boat & increment the `boatCount`, `left pointer` & decrement the `right pointer`\n\nLet\'s talk about what we\'ll do when we face **`possibility no. 2.0`**\nIf that\'s the case then we only one person will go in boat & increment the `boatCount` & decrement the `right pointer`\n\n**We have talk a lot, now let\'s see it\'s visually:**\n\n![image](https://assets.leetcode.com/users/images/9e87616c-e184-431e-b3ae-d6a2d5f74e60_1648087079.261476.gif)\n\n*I hope ladies - n - gentlemen approach is absolute clear,* **Let\'s code it up:**\n\n**Java**\n```\nclass Solution {\n    public int numRescueBoats(int[] people, int limit) {\n        int boatCount = 0;\n        Arrays.sort(people);\n        \n        int left = 0;\n        int right = people.length - 1;\n        \n        while(left <= right){\n            int sum = people[left] + people[right];\n            if(sum <= limit){\n                boatCount++;\n                left++;\n                right--;\n            }\n            else{\n                boatCount++;\n                right--;\n            }\n        }\n        return boatCount;\n    }\n}\n```\n**C++**\n```\nclass Solution {\npublic:\n    int numRescueBoats(vector<int>& people, int limit) {\n        int boatCount = 0;\n        sort(people.begin(), people.end());\n        \n        int left = 0;\n        int right = people.size() - 1;\n        \n        while(left <= right){\n            int sum = people[left] + people[right];\n            if(sum <= limit){\n                boatCount++;\n                left++;\n                right--;\n            }\n            else{\n                boatCount++;\n                right--;\n            }\n        }\n        return boatCount;\n    }\n};\n```\n\nANALYSIS:\n* **Time Complexity :-** BigO(NlogN)\n\n* **Space COmplexity :-** BigO(1)
['C', 'Java']