The lockdown is over, and the Ninja goes to see a movie. As the covid is not entirely over, Ninja decides to sit in a seat such that the distance between him and the closest person to him is maximized.
Given an array representing a row of ‘SEATS’, where ‘SEATS[i]’ = 1 represents a person sitting in the ith seat and ‘SEATS[i]’ = 0 represents the seat is empty, can you tell ninja the maximum possible distance to the closest person.
For example:You are given ‘SEATS’ = {1, 0, 1, 0 , 0, 0, 1}. The answer will be 2 because if the ninja sits on the ‘SEAT[4]’, then the person on ‘SEAT[2]’ or the person on ‘SEAT[6]’ will be at a maximum distance of 2. If the ninja sits on any other empty seat, the nearest person's distance will be less than 2.
The first line contains an integer 'T' which denotes the number of test cases.
The first line of each test case contains an integer ‘N’ representing the length of the ‘SEATS’ array.
The second line of each test case contains ‘N’ space-separated integers representing the ‘SEATS’ array.
Output Format:
For each test case, print a single integer, representing the maximum possible distance to the closest person.
The output of each test case will be printed in a separate line.
1 <= T <= 10
1 <= N <= 5000
0 <= SEATS[I] <= 1
Time limit: 1 sec
Note:
You do not need to input or print anything, as it has already been taken care of. Just implement the given function.
2
7
1 0 1 0 0 0 1
7
1 0 1 1 0 0 0
2
3
For the first test case, The answer will be 2 because if the ninja sits on the ‘SEAT[4]’, then the person on ‘SEAT[2]’ or the person on ‘SEAT[6]’ will be at a maximum distance of 2.
For the second test case, the answer will be 3 because if the ninja sits on the ‘SEAT[6]’, then the person on ‘SEAT[3]’ will be at a maximum distance of 3.
2
5
1 0 0 1 0
4
1 0 1 1
1
1
Try to calculate the maximum possible difference in both directions.
In this approach, we will find the maximum possible distance to the left and right for every seat. We will create two arrays, ‘LEFT’ and ‘RIGHT’, where ‘LEFT[I]’ stores the maximum possible distance to the nearest person from ‘SEAT[I]’ in the left direction similarly ‘RIGHT[I]’ stores the maximum possible distance to the nearest person from ‘SEAT[I]’ in the right direction. Now, the maximum possible distance to the nearest person from ‘SEAT[I]’ will be the minimum of ‘LEFT[I]’ and ‘RIGHT[I]’.
To construct ‘LEFT’ if ‘SEATS[I]’ is 1 set LEFT[I] as 0 otherwise ‘LEFT[I]’ will be ‘LEFT[I - 1]’ + 1. Similarly, To construct ‘RIGHT’ if ‘SEATS[I]’ is 1 set ‘RIGHT[I]’ as 0 otherwise ‘RIGHT[I]’ will be ‘RIGHT[I + 1]’ + 1.
We will find the maximum possible distance to the nearest person for every seat from 0 to ‘N’ - 1, and the maximum of them will be our final answer.
Algorithm:
O(N), where ‘N’ is the length of the input array 'SEATS'.
We are iterating the input array at most two times, once to create the array, ‘LEFT’ and the other to create the array, ‘RIGHT’. We are iterating arrays, ‘LEFT’ and ‘RIGHT’ only once. Hence the overall time complexity is O(N).
O(N), where ‘N’ is the length of the input array 'SEATS'.
The length of arrays, ‘LEFT’ and ‘RIGHT’ will be equal to the length of the input array i.e, ‘N’. Hence, the total time complexity will be O(N).