Ninja And The Class Room

Easy
0/40
Average time to solve is 12m
profile
Contributed by
4 upvotes
Asked in company
Wipro

Problem statement

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.
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
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.
Constraints:
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
Sample Input 1 :
2
5 2
1 0 0 0 0
2 1
0 1
Sample Output 1 :
1
0
Explanation For Sample Input 1 :
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.
Sample Input 2 :
2
4 1
1 1 1 1
2 1
0 0
Sample Output 2 :
0
1
Hint

Can we find all possible positions where students can be placed?

Approaches (1)
Greedy Approach

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: 
 

  • Push 0 at the start and end of an array ‘A’.
  • Iterate over an array ‘A’ from 0 to ‘A.size() - 1’ with iterator ‘i’
    • If ‘A[i] + A[i + 1] + A[i - 1] == 0’ then increment ‘i’ by 1 and decrement ‘K’ by 1’
  • Return ‘K <= 0’
Time Complexity

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).

Space Complexity

O(1).

 

As we are using constant extra space.

 

Hence, the space complexity is O(1).

Code Solution
(100% EXP penalty)
Ninja And The Class Room
Full screen
Console