Given a sorted array of food weights, split the array into two parts such that the total weight in the first part is as close to half the total weight as possible.
Explanation: Step-by-step: with input [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], we first calculate the total weight, which is 55. Then, we calculate the half weight, which is 27.5. We then split the array into two parts such that the total weight in the first part is as close to 27.5 as possible. The split can be done at index 5, resulting in the first part having a total weight of 27, which is as close to 27.5 as possible.
Explanation: Step-by-step: with input [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], we first calculate the total weight, which is 120. Then, we calculate the half weight, which is 60. We then split the array into two parts such that the total weight in the first part is as close to 60 as possible. The split can be done at index 7, resulting in the first part having a total weight of 59, which is as close to 60 as possible.
Master coding challenges related to Two Pointers and solve the [Backup] Astronaut Food Rationing problem optimally.
No dry run loaded.
🚀 Practice this problem
Run code, get AI hints & track streak