Open In App

Seating Arrangement without adjacent persons

Given an integer n, denoting the number of people who needs to be seated, and a list of m integer seats, where 0 represents a vacant seat and 1 represents an already occupied seat, the task is to find whether all n people can find a seat, provided that no two people can sit next to each other.

Examples:



Input: n = 2, m = 7, seats[] = {0, 0, 1, 0, 0, 0, 1}
Output: Yes
Explanation: The two people can sit at index 0 and 4.

Input:n = 1, m = 3, seats[] = {0, 1, 0}
Output: No
Explanation: There is no way to get a seat for one person.



Approach: This can be solved with the following idea:

The approach checks whether it is possible to find n consecutive seats in a row of m seats, where the seat array represents the row. It iterates through the seats array and checks if a seat is available, i.e., if the seat is not already occupied and there are no occupied seats adjacent to it. If a seat is available, it increments the available_seats counter and skips the next seat to avoid counting consecutive seats twice. Finally, it returns whether the number of available seats is greater than or equal to n.

Steps involved in the implementation of code:

Below is the code implementation of the above code:




// c++ Implemenatation
 
#include <iostream>
using namespace std;
 
class Solution {
public:
    // Function to check whether n people can be seated or not
    static bool is_possible_to_get_seats(int n, int m, int* seats) {
        int available_seats = 0;
        for (int i = 0; i < m; i++) {
            int prev = (i == 0) ? 0 : seats[i - 1];
            int next = (i == m - 1) ? 0 : seats[i + 1];
 
            // Increment the seat count
            if (prev + next + seats[i] == 0) {
                available_seats++;
                if (available_seats == n) {
                    return true;
                }
                i++;
            }
        }
        return false;
    }
};
 
// Driver code
int main() {
    int n = 2;
    int m = 7;
    int seats[] = { 0, 0, 1, 0, 0, 0, 1 };
 
    // Function call
    if (Solution::is_possible_to_get_seats(n, m, seats)) {
        cout << "Yes" << endl;
    }
    else {
        cout << "No" << endl;
    }
 
    return 0;
}




// Java Implemenatation
public class GFG {
    static class Solution {
 
        // Function to check whteher n
        // people can be sitted or not
        public static boolean
        is_possible_to_get_seats(int n, int m, int[] seats)
        {
            int available_seats = 0;
            for (int i = 0; i < m; i++) {
                int prev;
                if (i == 0)
                    prev = 0;
                else
                    prev = seats[i - 1];
                int next;
                if (i == m - 1)
                    next = 0;
                else
                    next = seats[i + 1];
 
                // Increment the seat count
                if (prev + next + seats[i] == 0) {
                    available_seats++;
                    if (available_seats == n) {
                        return true;
                    }
                    i++;
                }
            }
            return false;
        }
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 2;
        int m = 7;
        int[] seats = new int[] { 0, 0, 1, 0, 0, 0, 1 };
 
        // Function call
        if (Solution.is_possible_to_get_seats(n, m,
                                              seats)) {
            System.out.println("Yes");
        }
        else {
            System.out.println("No");
        }
    }
}




class Solution:
      # Function to check whether n people can be seated or not
    @staticmethod
    def is_possible_to_get_seats(n, m, seats):
        available_seats = 0
        for i in range(m):
              # Changing pev and next seats
            prev = seats[i - 1] if i != 0 else 0
            next = seats[i + 1] if i != m - 1 else 0
             
            # Increment the seat count
            if prev + next + seats[i] == 0:
                available_seats += 1
                if available_seats == n:
                    return True
                i += 1
        return False
 
# Driver code
n = 2
m = 7
seats = [0, 0, 1, 0, 0, 0, 1]
 
# Function call
if Solution.is_possible_to_get_seats(n, m, seats):
    print("Yes")
else:
    print("No")




using System;
 
class Solution
{
      // Function to check whether n people can be seated or not
    public static bool IsPossibleToGetSeats(int n, int m, int[] seats)
    {
        int availableSeats = 0;
        for (int i = 0; i < m; i++)
        {
            int prev = (i == 0) ? 0 : seats[i - 1];
            int next = (i == m - 1) ? 0 : seats[i + 1];
 
              // Increment the seat count
            if (prev + next + seats[i] == 0)
            {
                availableSeats++;
                if (availableSeats == n)
                {
                    return true;
                }
                i++;
            }
        }
        return false;
    }
}
 
class Program
{
      // Driver code
    static void Main()
    {
        int n = 2;
        int m = 7;
        int[] seats = { 0, 0, 1, 0, 0, 0, 1 };
 
          // Function call
        if (Solution.IsPossibleToGetSeats(n, m, seats))
        {
            Console.WriteLine("Yes");
        }
        else
        {
            Console.WriteLine("No");
        }
    }
}




<script>
 
class Solution {
    // Function to check whether n people can be seated or not
    static isPossibleToGetSeats(n, m, seats) {
        let availableSeats = 0;
        for (let i = 0; i < m; i++) {
            let prev = (i === 0) ? 0 : seats[i - 1];
            let next = (i === m - 1) ? 0 : seats[i + 1];
 
            // Increment the seat count
            if (prev + next + seats[i] === 0) {
                availableSeats++;
                if (availableSeats === n) {
                    return true;
                }
                i++;
            }
        }
        return false;
    }
}
 
// Driver code
const n = 2;
const m = 7;
const seats = [0, 0, 1, 0, 0, 0, 1];
 
// Function call
if (Solution.isPossibleToGetSeats(n, m, seats)) {
    console.log("Yes");
} else {
    console.log("No");
}
 
 
</script>
 
// code is contributed by shinjanpatra

Output
Yes






Time Complexity: O(m)
Auxilairy Space: O(1)


Article Tags :