Minimum days required to cure N persons
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++
#include <bits/stdc++.h>
using namespace std;
void daysToCure( int arr[], int N, int P)
{
int risk = 0;
int normal_risk = 0;
for ( int i = 0; i < N; i++) {
if (arr[i] >= 60 || arr[i] <= 10) {
risk++;
}
else {
normal_risk++;
}
}
int days = (risk / P) + (risk % P > 0)
+ (normal_risk / P)
+ (normal_risk % P > 0);
cout << days;
}
int main()
{
int arr[] = { 9, 80, 27, 72, 79 };
int N = sizeof (arr) / sizeof (arr[0]);
int P = 2;
daysToCure(arr, N, P);
return 0;
}
|
Java
class GFG
{
static void daysToCure( int arr[], int N, int P)
{
int risk = 0 ;
int normal_risk = 0 ;
for ( int i = 0 ; i < N; i++)
{
if (arr[i] >= 60 || arr[i] <= 10 )
{
risk++;
}
else
{
normal_risk++;
}
}
int days = (risk / P) + (normal_risk / P);
if (risk % P > 0 )
{
days++;
}
if (normal_risk % P > 0 )
{
days++;
}
System.out.print(days);
}
public static void main(String[] args) {
int arr[] = { 9 , 80 , 27 , 72 , 79 };
int N = arr.length;
int P = 2 ;
daysToCure(arr, N, P);
}
}
|
Python3
def daysToCure(arr, N, P):
risk = 0
normal_risk = 0
for i in range (N):
if (arr[i] > = 60 or arr[i] < = 10 ):
risk + = 1
else :
normal_risk + = 1
days = (risk / / P) + (risk % P > 0 ) + (normal_risk / / P) + (normal_risk % P > 0 )
print (days)
if __name__ = = '__main__' :
arr = [ 9 , 80 , 27 , 72 , 79 ]
N = len (arr)
P = 2
daysToCure(arr, N, P)
|
C#
using System;
class GFG
{
static void daysToCure( int [] arr, int N, int P)
{
int risk = 0;
int normal_risk = 0;
for ( int i = 0; i < N; i++)
{
if (arr[i] >= 60 || arr[i] <= 10)
{
risk++;
}
else
{
normal_risk++;
}
}
int days = (risk / P) + (normal_risk / P);
if (risk % P > 0)
{
days++;
}
if (normal_risk % P > 0)
{
days++;
}
Console.Write(days);
}
static void Main()
{
int [] arr = { 9, 80, 27, 72, 79 };
int N = arr.Length;
int P = 2;
daysToCure(arr, N, P);
}
}
|
Javascript
<script>
function daysToCure(arr , N , P) {
var risk = 0;
var normal_risk = 0;
for (i = 0; i < N; i++) {
if (arr[i] >= 60 || arr[i] <= 10) {
risk++;
} else {
normal_risk++;
}
}
var days = parseInt((risk / P) + (normal_risk / P));
if (risk % P > 0) {
days++;
}
if (normal_risk % P > 0) {
days++;
}
document.write(days);
}
var arr = [ 9, 80, 27, 72, 79 ];
var N = arr.length;
var P = 2;
daysToCure(arr, N, P);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
20 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...