Find a seat with Distance between nearest Occupied Seats maximised
Last Updated :
12 Nov, 2023
Given a string (seats) of 1s and 0s, where 1 represents a filled seat and 0 represents an empty seat in a row. Find an empty seat with maximum distance from an occupied seat. Return the maximum distance.
Examples:
Input: Seats = “1000101”
Output: 2
Explanation: Geek can take 3rd place and have a distance of 2 in left and 2 in right.
Input: Seats = “1000”
Output: 3
Explanation: Geek can take the rightmost seat to have a distance of 3.
Source: MakeMyTrip Interview Experience | On-Campus Virtual Experience
Approach:
The approach involves traversing the string while maintaining a count of consecutive empty seats. Whenever an occupied seat (‘1’) is encountered, the algorithm computes the distance and adjusts the maximum distance if required. In addition to considering the empty seats in the middle, it’s essential to account for both the initial and final empty seats to determine the maximum distance accurately.
Below is the implementation of the above approach.
C++
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
using namespace std;
int maxDistToClosest(string seats)
{
int max_distance = -1;
int consecutiveEmptySeats = 0;
for ( int i = 0; i < seats.length(); ++i) {
if (seats[i] == '0' ) {
consecutiveEmptySeats++;
}
else if (seats[i] == '1' && max_distance == -1) {
max_distance = consecutiveEmptySeats;
consecutiveEmptySeats = 0;
}
else {
max_distance
= max(max_distance,
static_cast < int >( ceil (
consecutiveEmptySeats / 2.0)));
consecutiveEmptySeats = 0;
}
}
max_distance = max(max_distance, consecutiveEmptySeats);
return max_distance;
}
int main()
{
string seats = "1000101";
cout << maxDistToClosest(seats) << endl;
return 0;
}
|
Java
public class Main {
public static int maxDistToClosest(String seats) {
int max_distance = - 1 ;
int consecutiveEmptySeats = 0 ;
for ( int i = 0 ; i < seats.length(); ++i) {
if (seats.charAt(i) == '0' ) {
consecutiveEmptySeats++;
} else if (seats.charAt(i) == '1' && max_distance == - 1 ) {
max_distance = consecutiveEmptySeats;
consecutiveEmptySeats = 0 ;
} else {
max_distance = Math.max(max_distance, ( int ) Math.ceil(consecutiveEmptySeats / 2.0 ));
consecutiveEmptySeats = 0 ;
}
}
max_distance = Math.max(max_distance, consecutiveEmptySeats);
return max_distance;
}
public static void main(String[] args) {
String seats = "1000101" ;
System.out.println(maxDistToClosest(seats));
}
}
|
Python3
import math
def maxDistToClosest(seats):
max_distance = - 1
ConsecutiveEmptySeats = 0
for i in range ( len (seats)):
if seats[i] = = '0' :
ConsecutiveEmptySeats + = 1
elif seats[i] = = '1' and max_distance = = - 1 :
max_distance = ConsecutiveEmptySeats
ConsecutiveEmptySeats = 0
else :
max_distance = max (max_distance, math.ceil(ConsecutiveEmptySeats / 2 ))
ConsecutiveEmptySeats = 0
max_distance = max (max_distance, ConsecutiveEmptySeats)
return max_distance
seats = '1000101'
print (maxDistToClosest(seats))
|
C#
using System;
public class GFG
{
public static int MaxDistToClosest( string seats)
{
int maxDistance = -1;
int consecutiveEmptySeats = 0;
for ( int i = 0; i < seats.Length; ++i)
{
if (seats[i] == '0' )
{
consecutiveEmptySeats++;
}
else if (seats[i] == '1' && maxDistance == -1)
{
maxDistance = consecutiveEmptySeats;
consecutiveEmptySeats = 0;
}
else
{
maxDistance = Math.Max(maxDistance,
( int )Math.Ceiling(consecutiveEmptySeats / 2.0));
consecutiveEmptySeats = 0;
}
}
maxDistance = Math.Max(maxDistance, consecutiveEmptySeats);
return maxDistance;
}
public static void Main( string [] args)
{
string seats = "1000101" ;
Console.WriteLine(MaxDistToClosest(seats));
}
}
|
Javascript
function maxDistToClosest(seats) {
let maxDistance = -1;
let consecutiveEmptySeats = 0;
for (let i = 0; i < seats.length; ++i) {
if (seats[i] === '0' ) {
consecutiveEmptySeats++;
}
else if (seats[i] === '1' && maxDistance === -1) {
maxDistance = consecutiveEmptySeats;
consecutiveEmptySeats = 0;
}
else {
maxDistance = Math.max(
maxDistance,
Math.ceil(consecutiveEmptySeats / 2)
);
consecutiveEmptySeats = 0;
}
}
maxDistance = Math.max(maxDistance, consecutiveEmptySeats);
return maxDistance;
}
const seats = "1000101" ;
console.log(maxDistToClosest(seats));
|
Time Complexity: O(N), where n is the number of seats.
Auxiliary Space: O(1).
Share your thoughts in the comments
Please Login to comment...