Program to check programmers convention arrangements are correct or not in Python


Suppose we have a number n, this is representing programmers looking to enter a convention, and we also have a list of number, convention 1 represents a programmer and 0 represents empty space. Now the condition is no two programmers can sit next to each other, we have to check whether all n programmers can enter the convention or not.

So, if the input is like n = 2, convention = [0, 0, 1, 0, 0, 0, 1], then the output will be True

To solve this, we will follow these steps −

  • for i in range 0 to size of conv, do
    • a:= 0 when i-1 < 0 otherwise i-1
    • b:= size of conv -1 when i+1 >= size of conv otherwise i+1
    • if conv[i] is same as 0 and conv[a] is same as 0 and conv[b] is same as 0, then
      • conv[i]:= 1
      • n := n - 1
  • return true when n <= 0, otherwise 0.

Example

 Live Demo

class Solution:
   def solve(self, n, conv):
      for i in range(len(conv)):
         a=0 if i-1<0 else i-1
         b=len(conv)-1 if i+1>=len(conv) else i+1
         if conv[i]==0 and conv[a]==0 and conv[b]==0:
            conv[i]=1
            n-=1
      return n<=0
ob = Solution()
n = 2
convention = [0, 0, 1, 0, 0, 0, 1]
print(ob.solve(n, convention))

Input

2, [0, 0, 1, 0, 0, 0, 1]

Output

True

Updated on: 06-Oct-2020

54 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements