Given an array arr[], representing the ages of N persons in a hospital and there is only one doctor who can give an immune dose to at most P persons per day, the task is to find the minimum count of days required to give the cure such that a high risk person and a normal person does not get a dose on the same day.
Note: Any person with age ≤ 10 or ≥ 60 is contemplated as a high risk person.
Examples:
Input: arr[] = {9, 80, 27, 72, 79}, P = 2
Output: 3
Explanation:
There are 4 high risk persons. Therefore, on days 1 and 2, 2 high risk persons can be immuned. On the last day, the normal person can be immuned. Therefore, 3 days are required.
Approach: Follow the steps below to solve the problem:
- Initialize a variable, say risk, to store the count of persons whose age is less than or equal to 10 and greater than or equal to 60.
- Initialize a variable, say normal_risk, to store the count of persons whose age is in the range [11, 59]
- Traverse the array and check if the age is less than or equal to 10 or greater than or equal to 60 or not. If found to be true, then increment the value of risk.
- Otherwise, increment the value of normal_risk.
- Finally, print the value of ceil(risk / P) + ceil(normal_risk / P)
Below is the implementation of the above approach:
C++
// C++ Program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find minimum count of days required // to give a cure such that the high risk person // and risk person does not get a dose on same day. void daysToCure( int arr[], int N, int P) { // Stores count of persons whose age is // less than or equal to 10 and // greater than or equal to 60. int risk = 0; // Stores the count of persons // whose age is in the range [11, 59] int normal_risk = 0; // Traverse the array arr[] for ( int i = 0; i < N; i++) { // If age less than or equal to 10 // or greater than or equal to 60 if (arr[i] >= 60 || arr[i] <= 10) { // Update risk risk++; } else { // Update normal_risk normal_risk++; } } // Calculate days to cure risk // and normal_risk persons int days = (risk / P) + (risk % P > 0) + (normal_risk / P) + (normal_risk % P > 0); // Print the days cout << days; } // Driver Code int main() { // Given array int arr[] = { 9, 80, 27, 72, 79 }; // Size of the array int N = sizeof (arr) / sizeof (arr[0]); // Given P int P = 2; daysToCure(arr, N, P); return 0; } |
Java
// Java Program for the above approach class GFG { // Function to find minimum count of days required // to give a cure such that the high risk person // and risk person does not get a dose on same day. static void daysToCure( int arr[], int N, int P) { // Stores count of persons whose age is // less than or equal to 10 and // greater than or equal to 60. int risk = 0 ; // Stores the count of persons // whose age is in the range [11, 59] int normal_risk = 0 ; // Traverse the array arr[] for ( int i = 0 ; i < N; i++) { // If age less than or equal to 10 // or greater than or equal to 60 if (arr[i] >= 60 || arr[i] <= 10 ) { // Update risk risk++; } else { // Update normal_risk normal_risk++; } } // Calculate days to cure risk // and normal_risk persons int days = (risk / P) + (normal_risk / P); if (risk % P > 0 ) { days++; } if (normal_risk % P > 0 ) { days++; } // Print the days System.out.print(days); } public static void main(String[] args) { // Given array int arr[] = { 9 , 80 , 27 , 72 , 79 }; // Size of the array int N = arr.length; // Given P int P = 2 ; daysToCure(arr, N, P); } } // This code is contributed by divyeshrabadiya07 |
Python3
# Python3 Program for the above approach # Function to find minimum count of days required # to give a cure such that the high risk person # and risk person does not get a dose on same day. def daysToCure(arr, N, P): # Stores count of persons whose age is # less than or equal to 10 and # greater than or equal to 60. risk = 0 # Stores the count of persons # whose age is in the range [11, 59] normal_risk = 0 # Traverse the array arr[] for i in range (N): # If age less than or equal to 10 # or greater than or equal to 60 if (arr[i] > = 60 or arr[i] < = 10 ): # Update risk risk + = 1 else : # Update normal_risk normal_risk + = 1 # Calculate days to cure risk # and normal_risk persons days = (risk / / P) + (risk % P > 0 ) + (normal_risk / / P) + (normal_risk % P > 0 ) # Print the days print (days) # Driver Code if __name__ = = '__main__' : # Given array arr = [ 9 , 80 , 27 , 72 , 79 ] # Size of the array N = len (arr) # Given P P = 2 daysToCure(arr, N, P) # This code is contributed by mohit kumar 29. |
C#
// C# Program for the above approach using System; class GFG { // Function to find minimum count of days required // to give a cure such that the high risk person // and risk person does not get a dose on same day. static void daysToCure( int [] arr, int N, int P) { // Stores count of persons whose age is // less than or equal to 10 and // greater than or equal to 60. int risk = 0; // Stores the count of persons // whose age is in the range [11, 59] int normal_risk = 0; // Traverse the array arr[] for ( int i = 0; i < N; i++) { // If age less than or equal to 10 // or greater than or equal to 60 if (arr[i] >= 60 || arr[i] <= 10) { // Update risk risk++; } else { // Update normal_risk normal_risk++; } } // Calculate days to cure risk // and normal_risk persons int days = (risk / P) + (normal_risk / P); if (risk % P > 0) { days++; } if (normal_risk % P > 0) { days++; } // Print the days Console.Write(days); } // Driver code static void Main() { // Given array int [] arr = { 9, 80, 27, 72, 79 }; // Size of the array int N = arr.Length; // Given P int P = 2; daysToCure(arr, N, P); } } // This code is contributed by divyesh072019. |
Javascript
<script> // javascript Program for the above approach // Function to find minimum count of days required // to give a cure such that the high risk person // and risk person does not get a dose on same day. function daysToCure(arr , N , P) { // Stores count of persons whose age is // less than or equal to 10 and // greater than or equal to 60. var risk = 0; // Stores the count of persons // whose age is in the range [11, 59] var normal_risk = 0; // Traverse the array arr for (i = 0; i < N; i++) { // If age less than or equal to 10 // or greater than or equal to 60 if (arr[i] >= 60 || arr[i] <= 10) { // Update risk risk++; } else { // Update normal_risk normal_risk++; } } // Calculate days to cure risk // and normal_risk persons var days = parseInt((risk / P) + (normal_risk / P)); if (risk % P > 0) { days++; } if (normal_risk % P > 0) { days++; } // Print the days document.write(days); } // Given array var arr = [ 9, 80, 27, 72, 79 ]; // Size of the array var N = arr.length; // Given P var P = 2; daysToCure(arr, N, P); // This code contributed by Rajput-Ji </script> |
3
Time Complexity: O(N)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!