`{"id":"3440a4ba-5167-4415-ad7f-df67ac63b5d7","name":"Boats To Save People","description":"1. You are given an array people where people[i] is the weight of the ith person.\r\n2. You have infinite number of boats where each boat can carry a maximum weight of limit. \r\n3. Each boat carries at most two people at the same time, provided the sum of the weight of those people is at most limit.\r\n4. Return the minimum number of boats to carry every given person.\r\n","inputFormat":"people = [3,2,2,1], limit = 3\r\n","outputFormat":"3\r\nExplanation: 3 boats (1, 2), (2) and (3)","constraints":"1. 1 &lt;= people.length &lt;= 5 * 10^4\r\n2. 1 &lt;= people[i] &lt;= limit &lt;= 3 * 10^4\r\n","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nint numRescueBoats(vector<int> &people, int limit) {\n //write your code here\n}\n\nint main(){\n int n, limit;\n cin>>n;\n vector<int> people(n);\n\n for(int i=0; i<n; ++i){\n cin>>people[i];\n }\n \n cin>>limit;\n int boats=numRescueBoats(people, limit);\n cout<<boats;\n}"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n\r\n // ~~~~~~~~~~~User''s Section~~~~~~~~~~~~\r\n public static int numRescueBoats(int[] people, int limit) {\r\n // write your code here\r\n }\r\n\r\n // ~~~~~~~~~~Input Management~~~~~~~~~~~\r\n public static void main(String[] args) {\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n int[] people = new int[n];\r\n\r\n for (int i = 0; i < n; i++)\r\n people[i] = scn.nextInt();\r\n\r\n int limit = scn.nextInt();\r\n int boats = numRescueBoats(people, limit);\r\n System.out.println(boats);\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"4\r\n3 2 2 1\r\n3","sampleOutput":"3","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"35f2cfb0-6f25-4967-b0c9-92f2384b9260","name":"Arrays And Strings For Intermediate","slug":"arrays-and-strings-for-intermediate-732","type":0},{"id":"96731596-2894-4754-b536-7cc19c587838","name":"Boats To Save People","slug":"boats-to-save-people","type":1}],"next":{"id":"78e11ef6-c94c-4c28-9bab-8e514636e127","name":"Boats To Save People","type":3,"slug":"boats-to-save-people"},"prev":{"id":"d34810ef-f9a1-4e18-a153-034f54e39e2f","name":"Construct Target Array With Multiple Sums","type":3,"slug":"construct-target-array-with-multiple-sums"}}}`

# Boats To Save People

1. You are given an array people where people[i] is the weight of the ith person. 2. You have infinite number of boats where each boat can carry a maximum weight of limit. 3. Each boat carries at most two people at the same time, provided the sum of the weight of those people is at most limit. 4. Return the minimum number of boats to carry every given person.

`{"id":"3440a4ba-5167-4415-ad7f-df67ac63b5d7","name":"Boats To Save People","description":"1. You are given an array people where people[i] is the weight of the ith person.\r\n2. You have infinite number of boats where each boat can carry a maximum weight of limit. \r\n3. Each boat carries at most two people at the same time, provided the sum of the weight of those people is at most limit.\r\n4. Return the minimum number of boats to carry every given person.\r\n","inputFormat":"people = [3,2,2,1], limit = 3\r\n","outputFormat":"3\r\nExplanation: 3 boats (1, 2), (2) and (3)","constraints":"1. 1 &lt;= people.length &lt;= 5 * 10^4\r\n2. 1 &lt;= people[i] &lt;= limit &lt;= 3 * 10^4\r\n","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nint numRescueBoats(vector<int> &people, int limit) {\n //write your code here\n}\n\nint main(){\n int n, limit;\n cin>>n;\n vector<int> people(n);\n\n for(int i=0; i<n; ++i){\n cin>>people[i];\n }\n \n cin>>limit;\n int boats=numRescueBoats(people, limit);\n cout<<boats;\n}"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n\r\n // ~~~~~~~~~~~User''s Section~~~~~~~~~~~~\r\n public static int numRescueBoats(int[] people, int limit) {\r\n // write your code here\r\n }\r\n\r\n // ~~~~~~~~~~Input Management~~~~~~~~~~~\r\n public static void main(String[] args) {\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n int[] people = new int[n];\r\n\r\n for (int i = 0; i < n; i++)\r\n people[i] = scn.nextInt();\r\n\r\n int limit = scn.nextInt();\r\n int boats = numRescueBoats(people, limit);\r\n System.out.println(boats);\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"4\r\n3 2 2 1\r\n3","sampleOutput":"3","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"35f2cfb0-6f25-4967-b0c9-92f2384b9260","name":"Arrays And Strings For Intermediate","slug":"arrays-and-strings-for-intermediate-732","type":0},{"id":"96731596-2894-4754-b536-7cc19c587838","name":"Boats To Save People","slug":"boats-to-save-people","type":1}],"next":{"id":"78e11ef6-c94c-4c28-9bab-8e514636e127","name":"Boats To Save People","type":3,"slug":"boats-to-save-people"},"prev":{"id":"d34810ef-f9a1-4e18-a153-034f54e39e2f","name":"Construct Target Array With Multiple Sums","type":3,"slug":"construct-target-array-with-multiple-sums"}}}`

Editor

# Boats To Save People

easy

1. You are given an array people where people[i] is the weight of the ith person. 2. You have infinite number of boats where each boat can carry a maximum weight of limit. 3. Each boat carries at most two people at the same time, provided the sum of the weight of those people is at most limit. 4. Return the minimum number of boats to carry every given person.

## Constraints

1. 1 <= people.length <= 5 * 10^4 2. 1 <= people[i] <= limit <= 3 * 10^4

## Format

### Input

people = [3,2,2,1], limit = 3

### Output

3 Explanation: 3 boats (1, 2), (2) and (3)

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}4 3 2 2 1 3```

### Sample Output

`.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}3`

Discussions

Show Discussion

Related Resources