Ninja has been asked to make the seating arrangement during the exam. Given an array βAβ of βNβ integers where βA[i] = 1β means there is a student seated at the index βiβ and βA[i] = 0β means that the place is currently unoccupied.
Provided no two students can seat adjacent to each other Ninjas has been asked to place extra βKβ students in the classroom.
Return 1 if the Ninja is able to place that extra βKβ student else return 0.
Example :Input: βNβ = 7, βKβ = 2, βAβ = [1, 0, 0, 0, 1, 0, 0]
Output: 1
It is possible to place locations 2 and 6 (0-indexed) the updated array will be [1, 0, 1, 0, 1, 0, 1] Here no two students are adjacent to each other.
The first line contains a single integer βTβ denoting the number of test cases, then the test case follows.
For each test case, the first line will contain two spaced integers βNβ, the size of an input array βAβ and βKβ. The second line will contain the βNβ spaced integers denoting the array βAβ values.
Output Format:
For each test case print, 1 if the Ninja is able to place that extra βKβ student else print 0.
Output for each test case will be printed on a separate line.
Note:
You are not required to print anything; it has already been taken care of. Just implement the function.
1 β€ T β€ 10
1 β€ N β€ 10^5
0 β€ K β€ 10^9
0 β€ βA[i]β β€ 1
It is guaranteed that the sum of βNβ is β€ 10^5 for all test cases.
Time limit: 1 sec
2
5 2
1 0 0 0 0
2 1
0 1
1
0
For test case 1:
It is possible to place at locations 2 and 4 (0-indexed) the updated array will be [1, 0, 1, 0, 1] Here no two students are adjacent to each other.
For test case 2:
For any possible combinations, there is no way to place the students.
2
4 1
1 1 1 1
2 1
0 0
0
1
Can we find all possible positions where students can be placed?
Approach:
We will simply iterate over an array and check for each 0 whether the adjacent elements of it are 0 or not. If they are also 0 then we will increment the count.
If the total count is at least βKβ then we will print 1 else 0.
We need to handle the case for the first and last elements separately.
Algorithm:
O(N), where βNβ is the size of the array βAβ.
As we are simply iterating over an array.
Hence, the time complexity is O(N).
O(1).
As we are using constant extra space.
Hence, the space complexity is O(1).