Mr Woods, an electrician has made some faulty connections of eight street lights in Timberland city.
The connections are such that if the street lights adjacent to a particular light are both ON
(represented as 1) or are both OFF (represented as 0), then that street light goes OFF the next
night. Otherwise, it remains On the next night. The two street lights at the end of the road have a
single adjacent street light, so the other adjacent light can be assumed to be always OFF. The
state of the lights on a particular day is considered for the next day and not for the same day.
Due to this fault, the people of the city are facing difficulty in during on the road at night. So they
have filed a complaint about this to the Head of the Federal Highway Administration. Based on this
complaint, the head has asked for the report of the state of street lights after M Days.
Write an algorithm to output the state of the street lights after the given M days.
Answers
Answer:
Mr Woods, an electrician has made some faulty connections of eight street lights in Timberland city.
The connections are such that if the street lights adjacent to a particular light are both ON
(represented as 1) or are both OFF (represented as 0), then that street light goes OFF the next
night. Otherwise, it remains On the next night. The two street lights at the end of the road have
Explanation:
Answer:
#include <iostream>
using namespace std;
void solve(int arr[], int n, int d)
{
int* newarr = new int[n];
while(d--)
{
int prev = 0;
for(int i = 0; i<n-1; i++)
{
if(prev == 0 && arr[i+1] == 0 || prev == 1 && arr[i+1] == 1)
{
newarr[i] = 0;
}
else
{
newarr[i] = 1;
}
prev = arr[i];
}
if(prev == 0)
{
newarr[n-1] = 0;
}
else
{
newarr[n-1] = 1;
}
for(int i= 0; i<n; i++)
{
arr[i] = newarr[i];
}
}
// for(int i = 0; i<n; i++)
// {
// cout<<arr[i]<<" ";
// }
// cout<<endl;
}
int main()
{
int n;
cin>>n;
int arr[n];
for(int i = 0; i<n; i++)
{
cin>>arr[i];
}
int d;
cin>>d;
solve(arr, n, d);
for(int i = 0; i<n; i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
return 0;
}