|
| 1 | +<h1 align="center">Check - If It is - Possible to - Servive on - Island</h1> |
| 2 | + |
| 3 | +## Problem Statement |
| 4 | + |
| 5 | +**Problem URL :** [Check If it is Possible to Servive on Island](https://www.geeksforgeeks.org/problems/check-if-it-is-possible-to-survive-on-island4922/0) |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +### Problem Explanation |
| 10 | +The task is to determine the minimum number of days required to prepare a certain amount of food for `S` days, where: |
| 11 | +- `N` is the number of food items that can be produced per day. |
| 12 | +- `M` is the number of food items needed per day. |
| 13 | + |
| 14 | +However, there are constraints: |
| 15 | +1. A week has 7 days, but only 6 days are available for food preparation (because 1 day is for rest). |
| 16 | +2. If the number of items required (`M`) per day exceeds the number of items produced per day (`N`), then it is impossible to fulfill the requirement. |
| 17 | + |
| 18 | +You need to compute the number of days it will take to produce the necessary food. If it's impossible, return `-1`. |
| 19 | + |
| 20 | +### Examples: |
| 21 | + |
| 22 | +#### Example 1: |
| 23 | +- **Input:** `S = 10, N = 5, M = 3` |
| 24 | +- **Explanation:** |
| 25 | + We need food for 10 days. Each day, we need 3 items, so the total food required is: |
| 26 | + ` |
| 27 | + text{totalFood} = 10 times 3 = 30 |
| 28 | + ` |
| 29 | + Each day, the production is 5 items. To calculate the days required: |
| 30 | + ` |
| 31 | + text{days} = frac{30}{5} = 6 , text{(exact)} |
| 32 | + ` |
| 33 | + So, it will take exactly 6 days to produce the required food. |
| 34 | + |
| 35 | +- **Output:** `6` |
| 36 | + |
| 37 | +#### Example 2: |
| 38 | +- **Input:** `S = 7, N = 3, M = 5` |
| 39 | +- **Explanation:** |
| 40 | + The total food required is: |
| 41 | + ` |
| 42 | + text{totalFood} = 7 times 5 = 35 |
| 43 | + ` |
| 44 | + Each day, we can produce 3 items. To calculate the days required: |
| 45 | + ` |
| 46 | + text{days} = frac{35}{3} approx 11.67 |
| 47 | + ` |
| 48 | + Since we cannot have a fraction of a day, we round up: |
| 49 | + ` |
| 50 | + text{days} = 12 |
| 51 | + ` |
| 52 | + So, it will take 12 days to produce 35 items. |
| 53 | + |
| 54 | +- **Output:** `12` |
| 55 | + |
| 56 | +#### Example 3: |
| 57 | +- **Input:** `S = 6, N = 5, M = 6` |
| 58 | +- **Explanation:** |
| 59 | + In this case, each day we need 6 items but we can only produce 5 items per day. Since we need more items than we can produce, it is impossible to fulfill the requirement. |
| 60 | + |
| 61 | +- **Output:** `-1` |
| 62 | + |
| 63 | + |
| 64 | +### Greedy Algorithm Approach: |
| 65 | + |
| 66 | +The problem can be solved using a greedy approach where: |
| 67 | +1. **Sort out impossible situations:** |
| 68 | + - If the number of items required per day (`M`) exceeds the number of items produced per day (`N`), then it is impossible to satisfy the requirements, so we return `-1`. |
| 69 | + - If the number of items required per day (`M`) is feasible, but the total food required exceeds the production capacity over the number of days (`7 * M > 6 * N`), we also return `-1`. |
| 70 | + |
| 71 | +2. **Find the minimum number of days:** |
| 72 | + - Once it's confirmed that it's feasible to fulfill the food requirement, calculate the total food required for `S` days (`totalFood = S * M`). |
| 73 | + - Divide the total food by the daily production (`N`), if the result is exact, return that number of days. |
| 74 | + - If not, round up the result to ensure enough food is produced. |
| 75 | + |
| 76 | +## Problem Solution |
| 77 | +```cpp |
| 78 | +class Solution { |
| 79 | +public: |
| 80 | + // Function to find the minimum number of days to prepare food |
| 81 | + int minimumDays(int S, int N, int M) { |
| 82 | + // Total food required for S days |
| 83 | + int totalFood = S * M; |
| 84 | + |
| 85 | + // Check if it is impossible to prepare food |
| 86 | + // Case 1: If the total food required exceeds the food production capacity (7 * M > 6 * N and S > 6) |
| 87 | + // Case 2: If the food required per day (M) exceeds the total food production capacity per day (N) |
| 88 | + if ((7 * M > 6 * N && S > 6) || M > N) return -1; |
| 89 | + |
| 90 | + // If the total food is divisible by N, return the exact number of days |
| 91 | + if (totalFood % N == 0) return totalFood / N; |
| 92 | + |
| 93 | + // If not, return the number of days needed, rounded up (totalFood / N + 1) |
| 94 | + else return totalFood / N + 1; |
| 95 | + } |
| 96 | +}; |
| 97 | +``` |
| 98 | +
|
| 99 | +## Problem Solution Explanation |
| 100 | +
|
| 101 | +1. **Total Food Calculation:** |
| 102 | + ```cpp |
| 103 | + int totalFood = S * M; |
| 104 | + ``` |
| 105 | + - We calculate the total number of food items required for `S` days, each requiring `M` food items. |
| 106 | + |
| 107 | +2. **Impossible Condition Check:** |
| 108 | + ```cpp |
| 109 | + if ((7 * M > 6 * N && S > 6) || M > N) return -1; |
| 110 | + ``` |
| 111 | + - We check if it's impossible to prepare the food: |
| 112 | + - If the total food required exceeds the food production capacity over the days (`7 * M > 6 * N`), or |
| 113 | + - If the food required per day (`M`) exceeds the food produced per day (`N`). |
| 114 | + - If either condition is true, we return `-1`, indicating that it is impossible to meet the requirement. |
| 115 | + |
| 116 | +3. **Exact Division of Total Food by Daily Production:** |
| 117 | + ```cpp |
| 118 | + if (totalFood % N == 0) return totalFood / N; |
| 119 | + ``` |
| 120 | + - If the total food required is perfectly divisible by the daily production (`N`), we return the exact number of days required (`totalFood / N`). |
| 121 | + |
| 122 | +4. **Round-up Division:** |
| 123 | + ```cpp |
| 124 | + else return totalFood / N + 1; |
| 125 | + ``` |
| 126 | + - If the total food is not perfectly divisible by `N`, we round up the division result to account for the remainder. This is done by adding 1 to the result of `totalFood / N`. |
| 127 | + |
| 128 | +### Example Walkthrough: |
| 129 | + |
| 130 | +#### Example 1: `S = 10, N = 5, M = 3` |
| 131 | +- **Total food required:** `10 * 3 = 30` |
| 132 | +- **Check for impossibility:** |
| 133 | + - `7 * M > 6 * N` is `7 * 3 > 6 * 5`, which is `21 > 30` (false). |
| 134 | + - `M > N` is `3 > 5` (false). |
| 135 | +- **Exact division:** `30 % 5 == 0`, so return `30 / 5 = 6`. |
| 136 | + |
| 137 | +#### Example 2: `S = 7, N = 3, M = 5` |
| 138 | +- **Total food required:** `7 * 5 = 35` |
| 139 | +- **Check for impossibility:** |
| 140 | + - `7 * M > 6 * N` is `7 * 5 > 6 * 3`, which is `35 > 18` (false). |
| 141 | + - `M > N` is `5 > 3` (false). |
| 142 | +- **Round-up division:** `35 % 3 != 0`, so `35 / 3 = 11.67` which rounds up to `12`. |
| 143 | + |
| 144 | +#### Example 3: `S = 6, N = 5, M = 6` |
| 145 | +- **Total food required:** `6 * 6 = 36` |
| 146 | +- **Check for impossibility:** |
| 147 | + - `7 * M > 6 * N` is `7 * 6 > 6 * 5`, which is `42 > 30` (true). |
| 148 | + - So, we return `-1` indicating that it's impossible to prepare the required food. |
| 149 | + |
| 150 | +### Time and Space Complexities: |
| 151 | + |
| 152 | +#### Time Complexity: |
| 153 | +- **Sorting:** The sorting operation takes `O(S log S)`, where `S` is the number of days (or the size of the input). |
| 154 | +- **Other operations:** The rest of the operations (such as simple arithmetic and comparisons) take constant time, `O(1)`. |
| 155 | + |
| 156 | +So, the total time complexity is: |
| 157 | +` |
| 158 | +O(S log S) |
| 159 | +` |
| 160 | + |
| 161 | +#### Space Complexity: |
| 162 | + |
| 163 | +- The space complexity is `O(1)` because no additional space is required, apart from the input parameters and a few integer variables. |
| 164 | + |
| 165 | +### Final Thoughts: |
| 166 | +This solution efficiently checks for impossible scenarios first and uses greedy logic to calculate the minimum number of days required. The sorting operation ensures that we handle the food production and requirement efficiently. |
0 commit comments