- Related Questions & Answers
- Program to check old and new version numbering are correct or not in Python
- Program to check whether parentheses are balanced or not in Python
- Program to check linked list items are forming palindrome or not in Python
- Program to check given push pop sequences are proper or not in python
- Program to check two strings are 0 or 1 edit distance away or not in Python
- Program to check all values in the tree are same or not in Python
- Program to check two parts of a string are palindrome or not in Python
- Program to check whether all leaves are at same level or not in Python
- Program to check two rectangular overlaps or not in Python
- C Program to check if two strings are same or not
- Program to check whether different brackets are balanced and well-formed or not in Python
- Program to check whether all palindromic substrings are of odd length or not in Python
- Program to check number of global and local inversions are same or not in Python
- Program to check whether leaves sequences are same of two leaves or not in python
- Program to check a string is palindrome or not in Python

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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.

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

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

True

Advertisements