Corona Spread Days
Corona Spread Days
PROBLEM STATEMENT :
- 1 represents a healthy person sitting in a chair.
- 0 represents the chair is empty.
- 2 represents the chair being occupied by a Coronavirus-infected person.
Each Corona virus-infected person will affect the healthy persons sitting adjacent to him/her so that the next day those affected will, in turn, start infecting their healthy adjacent persons. The program must print the number of days D required for the virus to stop spreading and the number of persons H who remain not infected by the virus in the end.
Boundary Condition(s):
2 <= N <= 20
Input Format:
The second line contains the N integer values separated by a space.
Output Format:
The first line contains D and H separated by a space.
Example Input/Output 1:
Input: ()
7
1 1 2 1 0 1 0
Output:
3 1
Explanation:
Here T = 10:05:45, X = 4, Y = 65 and Z = 100.
After adding 4 hours, the time becomes 14:05:45.
After adding 65 minutes, the time becomes 15:10:45.
After adding 100 seconds, the time becomes 15:12:25.
Example Input/Output 2:
Input: ()
7
1 2 1 1 1 1 1
Output:
6 0
1) LEARN THRICE
👇
2) THINK TWICE
👇
3) APPLY ONCE
SOLUTION :
C (Programming Language)
#include<stdio.h>
#include<stdlib.h>
int main()
{
int n;
scanf("%d",&n);
int x[n],i,j,d=1,c=1,h=0;
for(i=0;i<n;i++)
{
scanf("%d",&x[i]);
}
while(c!=0)
{
c=0;
for(i=0;i<n;i++)
{
if(x[i]==2)
{
if(x[i-1]==1&&i>0)
{
x[i-1]=2;
c=1;
}
if(x[i+1]==1&&i<n)
{
x[i+1]=2;
c=1;
i=i+1;
}
}
}
if(c==1)
d+=1;
}
for(j=0;j<n;j++)
{
if(x[j]==1)
h+=1;
}
printf("%d %d",d,h);
}
C++ (CPP)
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char** argv)
{
int n ;
cin >> n;
int arr[n] , empty = 0;
for(int i = 0 ; i<n ; i++)
{
cin >> arr[i];
}
int affected = 0 , days = 0;
while(1)
{
int flag = 0 ;
for(int i = 0 ; i<n ; i++)
{
if(arr[i]==2)
{
if(i-1>=0 && arr[i-1]==1)
{
arr[i-1]=2;
flag = 1;
}
if(i+1 < n && arr[i+1]==1)
{
arr[i+1] = 2;
i++;
flag = 1;
}
}
}
++days;
if(!flag)
break;
}
for(int i = 0 ; i<n ; i++)
empty+=(arr[i]==1);
cout << days << " " << empty;
}
JAVA
import java.util.*;
public class Hello {
public static void main(String[] args) {
Scanner io = new Scanner(System.in);
int n = io.nextInt();
int[] arr = new int[n+5];
arr[0]=arr[n+1]=-1;
for(int index=1;index<=n;index++){
arr[index]=io.nextInt();
}
int notAffected=0,daysReq=1,affected=0;
for(int ctr=0;ctr<n;ctr++,daysReq++,affected=0){
for(int index=1;index<=n;index++){
if(arr[index]==2){
if(arr[index-1]==1){
affected++;
arr[index-1]=2;
}
if(arr[index+1]==1){
affected++;
arr[index+1]=2;
index+=2;
}
}
}
if(affected==0){
for(int index=1;index<=n;index++){
if(arr[index]==1){
notAffected++;
}
}
System.out.print(daysReq+" "+notAffected);
break;
}
}
}
}
PYTHON
n=int(input())
l=list(map(int,input().split()))
x=0
while True:
i=0
flag=0
while i<n:
if i-1>=0 and l[i]==2 and l[i-1]==1:
l[i-1]=2
flag=1
if i+1<n and l[i]==2 and l[i+1]==1:
l[i+1]=2
flag=1
i+=1
i+=1
x+=1
if flag==0:
break
print(x,l.count(1))

Comments
Post a Comment