Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to check programmers convention arrangements are correct or not in Python
Suppose we have a number n representing programmers looking to enter a convention, and we also have a list of numbers where 1 represents a programmer and 0 represents empty space. The condition is that no two programmers can sit next to each other. We need 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 because we can place 2 more programmers without violating the adjacency rule.
Algorithm
To solve this, we will follow these steps ?
- Iterate through each position in the convention array
- For each empty position (0), check if both adjacent positions are also empty
- If safe to place a programmer, mark the position as occupied and decrement n
- Return True if all n programmers can be placed, False otherwise
Example
Let's implement the solution to check if programmers can be arranged properly ?
class Solution:
def solve(self, n, conv):
for i in range(len(conv)):
# Check left neighbor (use index 0 if at boundary)
a = 0 if i-1 < 0 else i-1
# Check right neighbor (use last index if at boundary)
b = len(conv)-1 if i+1 >= len(conv) else i+1
# If current position and neighbors are empty, place programmer
if conv[i] == 0 and conv[a] == 0 and conv[b] == 0:
conv[i] = 1
n -= 1
return n <= 0
# Test the solution
ob = Solution()
n = 2
convention = [0, 0, 1, 0, 0, 0, 1]
result = ob.solve(n, convention)
print(f"Can place {n} programmers: {result}")
print(f"Final arrangement: {convention}")
The output of the above code is ?
Can place 2 programmers: True Final arrangement: [1, 0, 1, 0, 1, 0, 1]
How It Works
The algorithm uses a greedy approach:
- It scans the array from left to right
- For boundary positions (first and last), it only checks the immediate neighbor
- For middle positions, it checks both left and right neighbors
- When a safe position is found, it immediately places a programmer there
Alternative Implementation
Here's a cleaner version without modifying the original array ?
def can_place_programmers(n, convention):
# Create a copy to avoid modifying original
conv = convention.copy()
placed = 0
for i in range(len(conv)):
if conv[i] == 0:
# Check if position is safe (no adjacent programmers)
left_safe = (i == 0) or (conv[i-1] == 0)
right_safe = (i == len(conv)-1) or (conv[i+1] == 0)
if left_safe and right_safe:
conv[i] = 1
placed += 1
if placed == n:
return True
return placed >= n
# Test cases
test_cases = [
(2, [0, 0, 1, 0, 0, 0, 1]),
(3, [0, 0, 0, 0, 0]),
(1, [1, 0, 1]),
(2, [0, 1, 0])
]
for n, conv in test_cases:
result = can_place_programmers(n, conv)
print(f"n={n}, convention={conv} ? {result}")
The output of the above code is ?
n=2, convention=[0, 0, 1, 0, 0, 0, 1] ? True n=3, convention=[0, 0, 0, 0, 0] ? False n=1, convention=[1, 0, 1] ? False n=2, convention=[0, 1, 0] ? False
Conclusion
This greedy algorithm efficiently determines if n programmers can be seated with the no-adjacent constraint. The time complexity is O(m) where m is the length of the convention array, making it suitable for real-time placement decisions.
