Question :There are 8 peoples which are standing in a row. They
can have two states on a respective day: Inactive(0) or Active(1). We
were given an array of 8 elements which shows the states of these people today
and we have to calculate the state of these people after the given no. of days.
Assumption 1: State of person will be Inactive on the next day if
both the adjacent persons are having the Active state or Inactive state today.
Assumption 2: State of person will be Active on the next day if one
adjacent person is having Active state and other adjacent person is having
Inactive state or vice versa.
Assumption 3: Person on the extreme left and extreme right have
only one adjacent person, so we can imagine, the other adjacent person is
Inactive.
Ex- 1
Input 1: [0,1,1,0,0,1,0,1], 1
Output 1: [1,1,1,1,1,0,0,0]
Ex-2
Input 2: [0,1,0,1,1,0,1,1], 2
Output 2: [0,1,1,1,1,0,1,1]
/* After day 1, states will be like this
[1,0,0,1,1,0,1,1]
#include <iostream> using namespace std; void equal(int *arr1, int *arr2, int n) { for(int i=0;i<n;i++) { arr1[i]=arr2[i]; } } void changestate(int *arr, int n, int days) { int *tmp=(int*)malloc(n*sizeof(int)); for(int i=0;i<days;i++) { for(int j=0;j<n;j++) { if(j==0) { tmp[j]=arr[j+1]; } else if(j>0&&j<n-1) { if(arr[j-1]==arr[j+1]) tmp[j]=0; else tmp[j]=1; } else { tmp[j]=arr[j-1]; } } equal(arr,tmp,n); //Copy the tmp array in arr } } int main() { int *state, n,days; cout<<"Enter the no. of states: "; cin>>n; cout<<"\nEnter the "<<n<<" states: "; state = (int*)malloc(n*sizeof(int)); for(int i=0;i<n;i++) { cin>>state[i]; } cout<<"\nEnter the no. of days: "; cin>>days; cout<<"\n\nOutput: "; changestate(state,n,days); for(int i=0;i<n;i++) { cout<<state[i]<<" "; } return 0; }