Maintaining Maximum Social Distance So I wrote this simple program to place a certain number of people on a list of seats whilst maintaining maximum social distance.
Given that there is at least one empty seat, calculate the maximum.distance from an empty seat to the closest occupied seat and return the available seat.
How can this be improved to be better
// seats.cpp

#include &lt;iostream&gt;
#include &lt;array&gt;

constexpr int N = 50;

int traverse_right(const std::array&lt;int, N&gt; &amp;seats, int i)
{
    if (i &lt; 0)
        return 0;
    else if(seats[i] == 1)
        return i;
    else return traverse_right(seats, --i);
}

int traverse_left(const std::array&lt;int, N&gt; &amp;seats, int i)
{
    if (i &gt;= seats.size())
        return 0;
    else if(seats[i] == 1)
        return i;
    else return traverse_left(seats, ++i);
}

int get_available_seat(const std::array&lt;int, N&gt; &amp;seats)
{
    int max = 0;
    int right_distance = 0;
    int left_distance = 0;
    int available_seat = -1;

    for(size_t i = 0; i != seats.size(); ++i)
    {
        if(seats[i] == 0)
        {
            right_distance = abs(i - traverse_right(seats, i));
            left_distance = abs(i - traverse_left(seats, i));
        }
        if(left_distance &gt; max &amp;&amp; right_distance &gt; max)
        {
            if(left_distance &lt;= right_distance &amp;&amp; left_distance != 0)
                max = left_distance;
            else 
                max = right_distance;
            available_seat = i;
        }
    }
    std::cout &lt;&lt; &quot;Maximum distance to available_seat is &quot; &lt;&lt; max &lt;&lt; '\n';
    return available_seat;
}

void display(const std::array&lt;int, N&gt; &amp;seats)
{
    for(const auto x : seats)
        std::cout &lt;&lt; x &lt;&lt; &quot; &quot;;
    std::cout &lt;&lt; '\n';
}
int main()
{
    std::array&lt;int, N&gt; seats{};
    for(int i = 0; i &lt; 12; ++i)
    {
        int available_seat  = get_available_seat(seats);
        seats[available_seat] = 1;
    }
    display(seats);
}


The code works as expected and though I didn't perform the check, user would need to test if there is an available seat before placing.
One idea would be to maintain a distance to the nearest seat, rather than simply an occupancy list (you can recover the same information since seats with zero distance are occupied). Then finding the seat with the furthest distance and updating the list reduces to:

finding the maximum element in the array
Traversing left and right from that element and updating the distances until the stored distance is already less than the distance to the newly added seat.

One more code review comment:
It may be nicer to either (1) template these functions on the parameter N, rather than declaring it at the beginning, so that they can be called later on a different sized array or (2) use std::vector so that the size of the seats container doesn't need to be known at compile time.